htaccess add a trailing slash to urls

Adding a trailing slash

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f #makes sure that files will not get a slash added.
RewriteCond %{REQUEST_URI} !(.*)/$ #url does not contain a trailing slash
RewriteRule ^(.*)$ http://www.domain.com/$1/ [L,R=301] # executes the rewrite rule

In case if some of the urs does not need a trailing slash added

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f #makes sure that files will not get a slash added.
RewriteCond %{REQUEST_URI} !example #makes sure that existing folder (example) will not get a slash added
RewriteCond %{REQUEST_URI} !(.*)/$ #url does not contain a trailing slash
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301] # executes the rewrite rule

Leave a Reply

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