CodeIgniter Forums

Full Version: Routing URLs and trailing slashes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a little problem with codeigniter because I want to have urls like that http://site.com/dogs/ or http://site.com/cats/ etc.

So see my routes.php file :
PHP Code:
$route['([0-9a-zA-Z-_+]+)'] = "site/categorie/$1"

My htaccess file :
Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]

My urls are working but I can access with this url http://site.com/dogs/ and this url http://site.com/dogs
But it's a big problem for duplicate content in SEO.

I already tried :
PHP Code:
$route['([0-9a-zA-Z-_+]+)/'] = "site/categorie/$1"

But it's doesn't work...

I am looking for a solution to for using trailing slashes for my urls.

Thank you for you help.
Try this htacces, with your first roule e.g.:

PHP Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteRule 
^(.*)$ index.php?/$[L
Nothing change with this rule Sad
In your e.g. you dont need a route. If you have the dogs controller with an index method and no parameter you can deactivate your route. Then your links should work.
did you try this?
Code:
RewriteRule ^(.*[^/])$ index.php?/$1/ [L]
(03-26-2015, 03:21 AM)Avenirer Wrote: [ -> ]did you try this?

Code:
RewriteRule ^(.*[^/])$ index.php?/$1/ [L]

... or rather this:

Code:
RewriteRule ^(.*)/?$ index.php?/$1/ [L]

Your rule would rejected requests that have a trailing slash.

Edit:

Actually, the OP's goal seems to be indeed to reject such requests. However, 'foo' being redirected to 'foo/' is not the same as both of them just returning the same content. Search engines should understand redirects and still only track 'foo/'.
The problem is that I need urls like that mypage.html and mycategorie/, I need both.

So look my my complete routes.php

PHP Code:
$route['default_controller'] = "site";
$route['404_override'] = 'site/error_404';
$route['contact.html'] = "site/contact";
$route['buy-([0-9a-zA-Z-_+]+).html'] = "site/buy/$1";
$route['([0-9a-zA-Z-_+]+)/([0-9a-zA-Z-_+]+).html'] = "site/article/$1/$2";
$route['([0-9a-zA-Z-_+]+).html'] = "site/page/$1";
$route['([0-9a-zA-Z-_+]+)'] = "site/categorie/$1"// for my pages like that categorie/ 

Code:
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?/$1/ [L]

Changing my htaccess doesn't change nothing Sad

I still have the same probleme, I can access to my page : http://site.com/categorie/ and http://site.com/categorie
There is any redirection.

I don't like my last roule :
PHP Code:
$route['([0-9a-zA-Z-_+]+)'] = "site/categorie/$1"// for my pages like that categorie/ 

But I see any other solution to make my categories like that categorie1/, categorie2/
If the only purpose is SEO:
1. Add a canonical link to your document head: http://moz.com/learn/seo/canonicalization
2. As much as possible, only use the canonical URLs when linking/redirecting within your own site.
Canonicale is a solution yes, but there isn't any solution with codeigniter routes and htaccess ? Sad