Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Trying to add ending slash to all urls. Here is the code:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !.html$
RewriteCond %{REQUEST_URI} !.xml$
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

It works BUT. When visiting http://www.example.com// you get a clone of index page. This is not good for seo. However http://www.example.com/articles// redirects fine to http://www.example.com/articles/ How to deal with double slash in index? Need help!

share|improve this question
add comment

migrated from stackoverflow.com Oct 25 '11 at 17:12

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 1 down vote accepted

Try this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
...
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]

You can add your exceptions where I wrote ... and it will redirect only if there is not already a trailing slash.

EDIT:

found this and verified that it works:

RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]

This should do what you are trying...

share|improve this answer
    
don't work... Same thing. –  Zhlobopotam Oct 25 '11 at 12:39
    
Brilliant! Thanks, Seybsen! Your second paste works. However I fixed it with this: RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L] But your code is more flexible. –  Zhlobopotam Oct 25 '11 at 14:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.