删除多个尾部斜杠 mod_rewrite
我知道这个问题仅在这个网站上就被问过很多次,但浏览相关帖子我找不到解决方案。尝试删除域后的多个尾部斜杠。以下 mod_rewrite 表达式似乎适用于 http://www 等 URL。 domain.com//path1///path2////,但不适用于域//
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]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
无法重现。即使在 DirectorySlashes 关闭的情况下,紧随域之后的额外斜杠也永远不会传递给 mod_rewrite ——我还没有检查是 Opera 还是 Apache 删除了斜杠。但除此之外,一切正常:
请求 http://localhost//abc/b//
注意:考虑不对主机进行硬编码:
还要注意内部的“//”没有被替换。您将添加另一个规则来替换内部斜杠。
新编辑:
好的,这似乎可以防止 URL 以 // 开头或结尾:
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 //:
这是似乎有效的完整列表:
Here is the complete listing that seems to be working:
您告诉 Apache 将所有内容(包括第一个斜杠)映射到 $1
RewriteRule ^(.*)$domain/$1 [R=301]
RewriteRule (.*)domain/$1? [R=301,L]
RewriteRule ^(.+)/$ $1 [R=301,L]
在插入符号后添加斜杠或删除域名后的斜杠
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
以下代码将删除所有额外的斜杠,包括域后面的额外斜杠。
The following code will strip all extra slashes including extra slashes after the domain.
这个在发送重定向之前删除所有斜杠
This one removes all the slashes before sending the redirect