4.3
(11)

Last Updated on February 24, 2022 by Amit

This article explains how to remove trailing slash from URLs using htaccess file.
If you want to do the opposite , then see How to enforce a trailing slash .

A trailing slash in any URL is a slash character that appears at the end of the URL. The following URL has a trailing slash at the end :
https://example.com/foo/bar/ .
If you don’t like your URLs with a slash character at the end , you can use RewriteRule directive in your htaccess file to remove it. And if your URLs do not have a trailing slash and you want to enforce a slash at the end then check this article .
Removing trailing slashes from URLs is important if your site URLs are indexed on search engines with a slash and you recently changed your URL structure where your new URLs don’t end with a / . To maintain the SEO it’s important to make a 301 redirect from old format to the new one.

Suppose , your site URLs look something like the following :
https://example.com/foobar/

And you want to change this URL to :

https://example.com/foobar .
This can be done with a 301 Redirect rule that matches all URLs containing a slash and redirect to the new URLs that do not have a slash.
The following simple rule can be used to remove trailing slashes from URLs . Just put this at the top of your htaccess file so that it can be read first by server :

RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^ %1 [L,R=301]

The following rule works same , the difference between this and the rule above is that we are checking the requested path in pattern of the RewriteRule instead of using RewriteCond directive .

RewriteEngine On
RewriteRule ^(.+)/$ /$1 [L,R=301]

With the rules above you can easily enforce no-traling slash on your site URLs. If the doesn’t work then try clearing your browser caches or use a different browser to test the redirection. If you still need my help with this, post a comment below .
Thanks for reading this article.

How useful was this post?

Click on a star to rate it!

Average rating 4.3 / 5. Vote count: 11

No votes so far! Be the first to rate this post.