Enforce trailing slash htaccess directive

Sitebee

Technical SEO
Staff member
For SEO reasons or aesthetic reasons, if you are looking to enforce the trailing / forward-slash at the end of the URL (URI) the below method is an easy solution to achieve the 'enforced trailing slash htaccess directive'.
Add the directive below to your htaccess file anywhere below the 'RewriteEngine on' opening directive.

Enforce 301 trailing slash htaccess directive
Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

Strip away the trailing slash htaccess directive
Code:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1  [R=301,L]

Please note* I would strongly recommend you use a sandbox/staging environment to test the directives prior to publishing to the live website. Please also be aware of SEO implications such as redirect chains and loops if you have other directives that could clash with the above.