0

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!

1 Answers1

1

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...

Seybsen
  • 125
  • 7
  • don't work... Same thing. –  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. –  Oct 25 '11 at 14:12