Add Trailing Slashes Without Hardcoding The Domain

There are lots of tutorials showing how to add trailing slashes to a URL with mod_rewrite, but 99.9% of them hard code the domain.

You might want to always append a trailing slash if you depend on it in $_SERVER['REDIRECT_URL'] or if you want to ensure that content is only available via a single URL for search engines etc.

Here’s the Apache mod_rewrite code:

<IfModule mod_rewrite.c>
RewriteEngine On

# Always append a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [R=301,L]

</IfModule>
Share the news:
  • Google Bookmarks
  • Facebook
  • RSS
  • StumbleUpon
  • Twitter
  • LinkedIn
  • Digg
  • del.icio.us

This entry was posted in Computers and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>