以下是我的重写规则:
###########
# 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]
一切正常(无论如何,任何提高性能或更好的regexp的建议都是受欢迎的)但是我遇到了一个奇怪的情况,我无法理解它是由我的重写规则还是默认的Apache行为产生的。 如果我的URL以“/”结尾,我可以在不重写的情况下附加任意数量的斜杠。
例如,如果在我的地址栏中插入以下内容:
的http:// [MY-HOST-NAME] ////////////////////////////
所有这些斜杠都没有删除。我仍然看到我的index.php页面。 如果我插入以下地址:
的http:// [MY-HOST-NAME] /构件///
所有这些多个斜杠都没有删除,我可以看到我的成员index.php页面。 等等...
是的,有人能帮帮我吗?非常感谢!答案 0 :(得分:3)
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R]
答案 1 :(得分:1)
# 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]
答案 2 :(得分:0)
Gerbens的答案适用于.htaccess但不适用于全局配置。 这个在发送重定向之前删除所有斜杠。
# 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]
答案 3 :(得分:0)
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