Htaccess: add/remove trailing slash from URL

Right below the RewriteEngine On line, add:

To enforce a no-trailing-slash policy.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

To enforce a trailing-slash policy:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

Leave a Reply

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