Welcome Guest, Not a member yet? Register   Sign In
Routing URLs and trailing slashes
#1
Information 

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.
Reply
#2

Try this htacces, with your first roule e.g.:

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

Reply
#3

Nothing change with this rule Sad
Reply
#4

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.

Reply
#5

(This post was last modified: 03-26-2015, 03:22 AM by Avenirer. Edit Reason: forgot something... )

did you try this?
Code:
RewriteRule ^(.*[^/])$ index.php?/$1/ [L]
Reply
#6

(This post was last modified: 03-26-2015, 04:11 AM by Narf.)

(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/'.
Reply
#7

(This post was last modified: 03-26-2015, 08:51 AM by peter.)

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/
Reply
#8

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.
Reply
#9

Canonicale is a solution yes, but there isn't any solution with codeigniter routes and htaccess ? Sad
Reply




Theme © iAndrew 2016 - Forum software by © MyBB