WebDesires

htaccess Remove Multiple Slashes in URL

htaccess Remove Multiple Slashes in URL

Need Some Help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request Support
There is a mobile optimized version of this page, view AMP Version.

Apache is a beast, a big untamed beast that sometimes releases a huge surprise that confuses, perplexes and even scares the living shit out of you, such as when you realise that Apache doesnt care about multiple slashes (empty directories?? WHAT ON EARTH?!?), which I have just coined #slashesgate.

So what is this seriously messed up and confusing issue in Apache you wonder? Maybe you noticed this yourself and found this article while trying to find a fix, lets start by explaining the issue…

Lets take an ordinary url such as: https://webdesires.co.uk/knowledge-base/htaccess-remove-multiple-slashes-in-url/

Here what #slashesgate can do:

https://webdesires.co.uk/////knowledge-base/htaccess-remove-multiple-slashes-in-url/

https://webdesires.co.uk/knowledge-base//////htaccess-remove-multiple-slashes-in-url/

https://webdesires.co.uk/knowledge-base/htaccess-remove-multiple-slashes-in-url//////

 

For some reason this behaviour is considered completely OK by Apache, what is worse is that it seems to just ignore the extra slashes and your website happily loads whatever normal page would exist without the multiple slashes. This presents a whole host of possible issues, most importantly concerning SEO attacks.

So can we stop #slashesgate? Yes we can! Just add these lines to your .htaccess file, and away we go.

<IfModule mod_rewrite.c>
    RewriteBase /

    # rule 1: removes multiple leading slashes directly after the domain name / TLD
    RewriteCond %{THE_REQUEST} \s/{2,}
    RewriteRule (.*) $1 [R=301,L]

    # rule 2: remove multiple slashes anywhere in the rest of the path
    RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
    RewriteRule (.*) %1/%2 [R=301,L]
</IfModule>

 

With the code above you can stop the issue by Removing Double Or More Slashes From URL By .htaccess.

Hope this article helps.

Need Some Help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request Support

Author: Dean Williams

Professional PHP Web Developer with expertise in OpenCart Web Development, WordPress Web Development, Bespoke Systems - also a seasoned Linux Server Administrator.