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

Google bot user agent
Google
Andy

Google Bot User Agents

Unsurprisingly, as they never do anything by half, there are quite a few different Google user agents. But, just how many there are might surprise

Read More »
What is Gutenburg?
Server Log Kid
Andy

What is Gutenburg?

Gutenburg is coming soon, it will be the default wordpress editor for writing blog posts and pages.

Read More »
useragent lookup
Server Logs
Andy

User Agent Lookup

Have you been analysing your Server logs and realised you are unsure about the user agent and what it means.  You can try and figure

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