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

Technical Seo Demystified - Martin Splitt
Conferences
Andy Halliday

Technical Seo Demystified: Martin Splitt

https://www.youtube.com/watch?v=X05ypQmLXc8Video can’t be loaded because JavaScript is disabled: Martin Splitt – Technical SEO Demystified | AngualrUP 2019 (https://www.youtube.com/watch?v=X05ypQmLXc8) This is a great talk from one

Read More »
changes to robots file
Crawling
Andy

Changes to Robots File

Google has been making some big announcements in the recent week about the robots.txt file but this one if one of the biggest announcements. If

Read More »

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.

* indicates required