Magento – Replacing multiple slashes in URL with a single slash

.htaccessredirect

I have the following htaccess rule that I would like to use in order to redirect www.example.com//test.html (any number of consecutive slashes) to www.example.com/test.html.

Options +FollowSymLinks
RewriteEngine on

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

It works on my local instance on MAMP, but, it will not redirect the multiple slashes unless there are more than 2 on a production environment (Amazon). So..

www.example.com//test.html --> www.example.com//test.html (doesn't work)

But

www.example.com///test.html --> www.example.com/test.html (works)

However, if there are multiple occurrences of multiple slashes, all of them are replaced by single slashes sometimes like below.

www.example.com//test//test.html --> www.example.com/test/test.html (works)
www.example.com//test/test.html --> www.example.com//test/test.html (doesn't work)

What's going on? I've read numerous posts on this, and none of them seem to work.

Best Answer

So you rely on webserver that it will always replace multiple slashes for (.*) which is not mandatory by the specification.

What you can do is to create multiple rules for 1,2,3 occurrences of multiple slashes, like

RewriteRule (.+)//+(.+) $1/$2 [r=301,l]

And so on. Start from 3, then 2, then 1 as the order is matter in this approach.

Not ideal, but might help.

Related Topic