Add or Remove trailing slash from URL by using .htaccess

Right below the RewriteEngine On line, add

To enforce a no-trailing-slash policy (To remove the trailing slash from URL).

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

To enforce a trailing-slash policy (To add the trailing slash at URL)

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

Leave a Reply