Issue In Removing Double Or More Slashes From URL By .htaccess

Rana picture Rana · Jun 13, 2013 · Viewed 27.2k times · Source

I am using the following htaccess rul to remove double or more slashes from web urls:

#remove double/more slashes in url
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

This is working fine for slashes occured in the middle of uris, such as, If use url:

http://demo.codesamplez.com/html5//audio

Its being redirected to proper single slahs url:

http://demo.codesamplez.com/html5/audio

But if the url contains double slashes in the beginning, JUST AFTER the domain name, then there its not working, example:

http://demo.codesamplez.com//html5/audio

its not being redirected.

How I can fix the above rule to work for this type of urls as well? Thanks.

Answer

Marcel picture Marcel · Aug 29, 2013

Give it a try with:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]

It should redirect to a single slash at the end of the domain. And an improvement on yours:

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