Using Htaccess: add/remove trailing slash from URL

Whether you want to keep the trailing or remove it that’s your decisions, but what is important is that decide upon one and keep to it.

Otherwise there is the possibility of having duplicate URLs and that can cause quite a few issues.

The easiest way to add a couple of lines of code to your htaccess file and it then you don’t need to worry about.

To enforce a no-trailing-slash policy

Right below the RewriteEngine On line in the htaccess file, add:

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

To enforce a trailing-slash policy:

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

It really is that simple.

My preference is no slash at the end, but thats just personal preference.

Share:

Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest
Share on linkedin
LinkedIn

Leave a Comment

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

On Key

More Great Articles

Subscribe to my newsletter

Want to learn more about technical SEO and Server Log Analysis, sign up below and I will teach you.Each week I will send you the most helpful articles delivered straight into your inbox as well as sharing some very useful tips and tricks to improve your technical SEO skills to Ninja level.

Subscribe to our mailing list

* indicates required