1

这是我的重写规则:

###########
# 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]

一切正常(无论如何,任何提高性能或更好的正则表达式的建议都是受欢迎的)但我遇到了一个奇怪的情况,我不明白它是由我的重写规则还是由默认的 Apache 行为产生的。如果我的 URL 以“/”结尾,我可以在不重写的情况下添加任意数量的斜杠。

例如,如果在我的地址栏中插入以下内容:

http://[MY-HOST-NAME]////////////////////////

所有这些斜线都没有被删除。而且我仍然看到我的 index.php 页面。如果我插入以下地址:

http://[MY-HOST-NAME]/members///

所有这些多个斜杠都没有被删除,我可以看到我的成员 index.php 页面。等等...

有人能帮助我吗?非常感谢!

4

4 回答 4

3
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R]
于 2011-12-11T14:47:21.750 回答
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]
于 2018-09-05T18:26:52.140 回答
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]
于 2013-06-18T19:47:31.627 回答
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
于 2013-06-18T19:50:57.980 回答