I know this question was asked a number of times on this site alone, but browsing through the relevant posts I couldn't find a solution. Trying to remove multiple trailing slashes after domain. The following mod_rewrite expressions seem to work for URLs such as http://www.domain.com//path1///path2////, but do not work for domain//
DirectorySlash Off
RewriteEngine on
# Canonical fix
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
RewriteRule ^/main.do http://www.domain.com/ [R=301,L]
RewriteRule ^/index.jsp http://www.domain.com/ [R=301,L]
# Remove bogus query strings
RewriteCond %{query_string} q= [NC]
RewriteRule (.*) http://www.domain.com/$1? [R=301,L]
# Remove multiple slashes after domain - DOESN'T WORK!!!
#RewriteCond %{REQUEST_URI} ^//+(.*)$ [OR]
#RewriteCond %{REQUEST_URI} ^(.*/)/+$
#RewriteRule / http://www.domain.com/%1 [R=301,L]
# Remove multiple slashes anywhere in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
# Externally redirect to get rid of trailing slash except for home page, ads
RewriteCond %{REQUEST_URI} !^/ads/
RewriteRule ^(.+)/$ $1 [R=301,L]
The following code will strip all extra slashes including extra slashes after the domain.
This one removes all the slashes before sending the redirect
Here is the complete listing that seems to be working:
You're telling Apache map everything including the first slash to $1
RewriteRule ^(.*)$ domain/$1 [R=301]
RewriteRule (.*) domain/$1? [R=301,L]
RewriteRule ^(.+)/$ $1 [R=301,L]
add a slash after the caret or drop the one after your domain name
Can't reproduce. Extra slashes immediately after the domain are never passed to mod_rewrite even with DirectorySlashes off -- I haven't checked whether it's Opera or Apache that's removing the slash). But otherwise, everything works fine:
Request for http://localhost//abc/b//
Note: consider not hard-coding the host:
Also note that the inner "//" was not replaced. You will to add another rule to replace inner slashes.
NEW EDIT: OK, this seems to work for preventing URLs starting or ending with //: