Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 you get a clone of index page. This is not good for seo. However redirects fine to How to deal with double slash in index? Need help!

share|improve this question

migrated from Oct 25 '11 at 17:12

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

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.


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 ^(.*)$$1 [R=301,L] But your code is more flexible. – Zhlobopotam Oct 25 '11 at 14:12

Your Answer


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