-2

Based on code found here: remove multiple trailing slashes mod_rewrite

I have the following htaccess

Options +FollowSymLinks
DirectorySlash Off
RewriteEngine on
RewriteOptions inherit
RewriteBase /

#
# remove multiple slashes from url
#
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]

#
# Remove multiple slashes anywhere in URL
#
RewriteCond %{THE_REQUEST} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

Yet i found out the G-Bot has crawled this url: http://www.example.com/aaa/bbb/////////bbb-ccc/bbb-ddd.htm. (aaa, bbb, ccc, ddd, are keywords in url, not to be taken litraly - i jut show the pattern of the url)

Testing the above url in by live server i found out that the slash removal does not work.

Anyone can offer any tips or improvement to the the existing code? Thank you

EDIT 1
@Sylwester provided the following code

# if match set environment variable and start over
RewriteRule ^(.*?)//+(.*)$ $1/$2 [E=REDIR:1,N]

# if done at least one. redirect with 301
RewriteCond %{ENV:REDIR} 1
RewriteRule ^/(.*) /$1 [R=301,L]

It is not working either. I still see the ////// inside the url.
I have put this set of rules at the very top of my htaccess file, right below the " RewriteBase /", so as not to be affected by other rules, yet... nothing.
Any other suggestion?

Community
  • 1
  • 1
andrew
  • 2,058
  • 2
  • 25
  • 33

1 Answers1

3

Per directory and .htaccess is tricky since apache actually have removed redundant slashed for us. Eg. there is no match for //+ anymore so we check the %{REQUEST_URI} since it has the original URI while the rewrite rule need to match anything:

# NB: Only works for per directory and .htaccess
# Needs "AllowOverride All" in global config for .htaccess 
RewriteEngine On
RewriteBase "/"

Options +FollowSymlinks
# Check if the REQUEST_URI has redundant slashes
# and redirect to self if it has (which apache has cleaned up already)
RewriteCond %{REQUEST_URI} //+
RewriteRule ^(.*) $1 [R=301,L]   

If you can add global config I would have prefered this in the virtual host instead:

RewriteEngine On
# if match set environment variable and start over
RewriteRule ^(.*?)//+(.*)$ $1/$2 [E=REDIR:1,N]

# if done at least one. redirect with 301
RewriteCond %{ENV:REDIR} 1
RewriteRule ^/(.*) /$1 [R=301,L]
Sylwester
  • 47,942
  • 4
  • 47
  • 79