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]


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 *