CodeIgniter - htaccess - Redirect - .htaccess

I am currently working with "Code Igniter" and the "i18n Multi-language Library Helper". My website is bilingual.
The simple task I want to do is to force redirect of this path:
domain.com/inscription
to
domain.com/fr/inscription
I tried to do it with CI Route Engine, but it it not working properly because the route engine will redirect to the current language ( ex. domain.com/en/inscription ), which should not work. Only domain.com/fr/inscription should work.
I believe the best way to do it if with the htaccess file, but I can't get it to work.

If you are using Codeigniter 2.x try using this library
http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n
Read the guide on how to set up the library and you can see there in MY_Lang.php file array like this. The first language in the array is the default one. So it will redirect you automatically to the default language
// languages
private $languages = array(
'en' => 'english',
'de' => 'german',
'fr' => 'french',
'nl' => 'dutch'
);
Hope this helps

Here is a code to redirect
# This allows you to redirect index.html to a specific subfolder
Redirect /inscription http://domain.com/fr/inscription
the line under # comment line is the code you need to paste

Related

GAE Application app.yaml VS .htaccess

How to Write a app yaml looks like htacess below
RewriteEngine on
# To append a query string part in the substitution string
RewriteRule ^([0-9a-z_/\-]+)/$ index.php\?p=$1 [QSA]
RewriteRule ^([0-9a-z_/\-]+)$ index.php\?p=$1 [QSA]
im doing so at app yaml for GAE Application was fail
as Dan mentioned, you will not be able to handle this all in the yaml, and will need to to handle the logic yourself, we do a simular thing in one of our project and will outline below our solution.
Our scenario is handling the old website article's URL structure, and trying to redirect them to the new URL structure.
In our yaml we register the pattern that we are looking to match on and direct it to a file where we will do the handling :
- url: (/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/.*) (Pattern to match on)
script: publication.custom.redirector.app (Path to your .py that will have your handling in)
In our .py file we will catch that pattern and route it to our DefaultHandler that can then do any logic you need and redirect out:
( in our project this goes to /publication/custom/redirector.py )
import request
import settings
import re
class DefaultHandler(request.handler):
def get(self, pre, year, month, day, post):
post = re.sub('(.*[^0-9])[\d]{1}$', r'\1', post)
post = re.sub('[^0-9a-zA-Z-_\/]+', '', post)
path = post.split("/")[-1]
slug = "{0}-{1}-{2}-{3}".format(year, month, day, path)
article = self.context.call('pub/articles/get', slug=slug.lower())
if article:
self.redirect(article['pub_url'], permanent=True)
else:
self.render("pages/page-not-found/page-not-found.html")
app = request.app([
('/(.*)/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)', DefaultHandler)
], settings.gaext.config)
Hope this helps
The GAE app.yaml doesn't have a URL rewrite capability, it just parses the incoming request URL for request routing purposes, to determine which handlers to invoke.
One could maybe argue that the static_file handlers configuration has a somewhat similar capability, but it is only applicable to the static assets.
For the dynamic handlers you'd need to take care of such "rewrite" inside your app code. I'm quoting "rewrite" here as technically it's just a different way of parsing/interpreting the request URL inside your app code - the original, unchanged request URL will still be the one recorded by the GAE infra.

How do I generate sitemap for dynamic links in expressjs?

I have a jobpage which has url as /jobpage/:categoryname/:companyname/:jobtitle/:jobid. Parameters are generated dynamically. I want all such dynamically generated links on sitemap. I have used express-sitemap package, code is as below -
var sitemap = require('express-sitemap');
sitemap({
sitemap: 'sitemap.xml', // path for .XMLtoFile
robots: 'robots.txt', // path for .TXTtoFile
generate: app, // option or function, is the same
sitemapSubmission: '/sitemap.xml', // path of sitemap into robots
url : 'xxxx',
map: {
'/jobpage': ['get'],
'/college': ['get'],
},
route: { // specific option for some route
'/jobpage': {
lastmod: '2016-04-25',
changefreq: 'weekly',
priority: 1.0,
},
},
}).toFile(); // write sitemap.xml and robots.txt
Sitemap is getting generated with link as
<url>
<loc>xxxx/jobpage/:categoryname/:companyname/:jobtitle/:jobid</loc>
</url>
How do I generate dynamic links? Any leads will be highly appreciated.
in my case i did it like below.
Create a separate file that sitemap_generator.js which actually read all database models which leads to pages.
then generate xml and write to web folder and in certain interval it keep updating xml as well.
it start creating sitemap when node server start. i did this manually because i found no automated solution comes with limitations.
i think most of time your business logic might not fit into any lib, because that libs can't know what dynamic pages can be. which you already knew.
https://www.npmjs.com/package/express-sitemap

Pass parameter to default controller/method with Yii urlManager

I would like to use a catch-all rule for urlManager that would pass anything after my base url to a default controller and method as parameters.
My goal would be for a url such as mysite.com/123 to map to mysite.com/controller/method/123 where controller/method are predetermined and 123 is passed as a named parameter.
Such a rule would be put last in the urlManager chain so that if none of the other rules match it would pass whatever is after the base url to my selected controller/method.
Any ideas??
Edit:
Adding a rule '<id>'=>'controller/method' (which I think I had tried anyhow) and then viewing site.com/123 would return a 404 not found, but from apache, NOT Yii. Something I did not take into consideration.
Going to mysite.com/index.php/123 got the desired result. Going to mysite.com/controller/method though would route the url properly. Strange...
Yes, you have to put this as the last rule under all other rules.
'<id>' => 'controllerName/methodName/<id>,'
Example:
'<id>' => 'user/view/<id>',
This will redirect all URLs like this:
mysite.com/1
To:
mysite.com/user/view/1
If you want to restrict to numbers only, use
'<id:\d+>' => 'controllerName/methodName/<id>,'
You should add this rule to bottom of url rules:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<pname:\w+>'=>'site/test',
),
),
Pname: your named parameter.
Site/test: the target action.
In your action you should define your "pname" as method paramter:
public function actionTest($pname) {
echo "Name:$pname";
}

Jquery ajax() messing with my .htaccess mod_rewrite

I am performing a simple AJAX() request using Jquery (Google hosted 1.7.1 jquery.min.js code)
The code is pretty simple:
$.ajax({
type: "POST",
url: "../inc/ajax_msgread_sendPM.php",
data: "fromuserid=<?php echo $fromuserid; ?>&pmSubject=<?php echo urlencode($pmSubject); ?>&pmBody=" + pmReply,
success: function(data){
$("#showSuccess").show("fast");
$("#resultResponse").html(data);
}
});
The mod_rewrite .htaccess for this document is:
RewriteRule ^messages/read/([^/]+)/([^/]+)/?$ /msgread.php?usernam=$1&keynode=$2 [QSA,L]
When I view the $resultResponse for some reason the Ajax keeps wanting to turn $1 into 'inc' so any unrelated (or related) mySQL queries using $_GET["usernam"] from the URL ends up returning 'inc'
Why is this happening? There is no relation between my script and the mod_rewrite. There are no variables named "usernam" or "1" on the script (or anywhere on the site).
Firebug gives no help.
Advice please?
UPDATE:
I see where the problem is coming from.. in the ajax jquery code:
url: "../inc/ajax_msgread_sendPM.php",
the "inc" keeps getting set as the username because of its location based on the mod_rewrite rules... I need to somehow exclude this from mod_rewrite... just not sure how to solve this problem
As long as current url for your page is
www.domain.com/messages/read/username/NQ
and you use relative path - it is being rewritten to
www.domain.com/messages/read/username/inc/ajax_msgread_sendPM.php
which is definitely not what you want. The simplest solution would be to change the ajax endpoint url to:
url: "/inc/ajax_msgread_sendPM.php",

HTACCESS - Block everything but specified SEO friendly URL

I haven't found all the answer to my current problem.
Here is the root of the site:
cache
img
display.php
admin.php
What I need is to block all the direct access of the files and allow only access via url formatted like that:
1 ht*p://sub.domain.com/image/param/size/folder/img.jpg (param, size, folder, img are parameters)
2 ht*p://sub.domain.com/action/param1/param2/ (param1, param2 are parameters)
1 would point to display.php with the correct parameters
2 would point to admin.php with the correct parameters
Every other access must be 404 (at best) or 403
my rules are (the htaccess is in ht*p://sub.domain.com/):
RewriteRule ^image/([^/]+)/([0-9]+)/([^/]+)/([^/]+)\.jpg display.php?param=$1&size=$2&folder=$3&img=$4 [L]
RewriteRule ^action/([^/]+)/([^/]+) admin.php?action=$1&param=$2 [L]
Those rules work as I want to but I am stuck on how to block any access that does not come from those URL!
Also (as a bonus) I would like to be able to use the same htaccess on diferrent web address without having to change this file.
Thanks in advance
Have you try moving the image out of the public folder and use php to call the image in?
For the PHP files you can use the switch statement (http://www.php.net/switch).
For the admin.php file you can do something like:
$get_action = $_GET['action'];
switch ($get_action) {
case "edit":
case "view":
case "delete":
case "add":
//Continue loading the page
break;
default:
header('HTTP/1.1 403 Forbidden');
die();
}
Note: I don't know how your code looks or works, but you can have an idea base on the code I added.

Resources