问题
Here are my rewrite rules:
###########
# Rewrite #
###########
# Settings
RewriteEngine On
RewriteBase /
# Cache Busting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} "^(.+)\.\d+\.(css|js)$" [NC]
RewriteRule "^.*$" "/%1.%2" [L]
# No Index
RewriteCond %{THE_REQUEST} "\ /.*index\.php.*\ " [NC]
RewriteRule "^(.*)index\..+$" "/$1" [L,NC,R=301]
# No Question Mark
RewriteCond %{THE_REQUEST} "\ /[^?]*\?\ "
RewriteRule "^(.*)$" "/$1?" [L,R=301]
# WWW
# RewriteCond %{HTTP_HOST} !"^(?:static|www)\.(.+)$" [NC]
# RewriteCond %{HTTPS}s "^on(s)|"
# RewriteRule "^(.*)$" http%2://www.%1/$1 [L,R=301]
Everything works fine (any suggestion to improve performances or for better regexps is welcome, anyway) but I'm experiencing a weird situation and I can't understand if it's produced by my rewrite rules or by a default Apache behavior. If my URL ends with a "/", I can append as many slashes as I want without it being rewritten.
For example, if in my address bar I insert the following:
http://[MY-HOST-NAME]////////////////////////////
All those slashes are not being removed. And I'm still seeing my index.php page. If I insert the following address:
http://[MY-HOST-NAME]/members///
All those multiple slashes are not being removed and I can see my members index.php page. And so on...
Can someone help me please? Many thanks!
回答1:
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R]
回答2:
# rule 1: remove multiple leading slashes (directly after the TLD)
RewriteCond %{THE_REQUEST} \s/{2,}
RewriteRule (.*) $1 [R=301,L]
# rule 2: remove multiple slashes in the requested path
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule (.*) %1/%2 [R=301,L]
回答3:
RewriteEngine on
RewriteBase /
#existing rule
#remove the www.
RewriteCond %{HTTP_HOST} ^www.website.co.uk$ [NC]
RewriteRule ^(.*)$ http://local.website.co.uk/$1 [R=301,L]
#new Rule
#if its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#and it has a trailing slash then redirect to URL without slash
RewriteRule ^(.+)/$ /$1 [L,R=301]
# rest of your existing rules go here
回答4:
Gerbens answer works well for .htaccess but not so much for global config. This one removes all the slashes before sending the redirect.
# 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]
来源:https://stackoverflow.com/questions/8460015/how-to-remove-multiple-trailing-slashes