删除多个尾部斜杠mod_rewrite [英] remove multiple trailing slashes mod_rewrite

查看:50
本文介绍了删除多个尾部斜杠mod_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道仅在此站点上就曾多次问过这个问题,但是在浏览相关帖子时我找不到解决方案.尝试删除域后的多个尾部斜杠.以下mod_rewrite表达式似乎适用于诸如 http://www之类的URL. domain.com//path1///path2////,但不适用于domain//

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]

推荐答案

无法复制.即使关闭DirectorySlashes,域之后也不会将多余的斜杠传递给mod_rewrite -我尚未检查是删除了斜杠还是Opera还是Apache.但是否则,一切都会正常进行:

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:

RewriteBase /
RewriteCond %{REQUEST_URI} ^//+(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*/)/+$
RewriteRule .* http://domain/%1 [R=301,L]

请求 http://localhost//abc/b//

127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (3) [perdir C:/HTTP/htdocs/] add path info postfix: C:/HTTP/htdocs/abc -> C:/HTTP/htdocs/abc/b/
127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (3) [perdir C:/HTTP/htdocs/] strip per-dir prefix: C:/HTTP/htdocs/abc/b/ -> abc/b/
127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (3) [perdir C:/HTTP/htdocs/] applying pattern '.*' to uri 'abc/b/'
127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (4) [perdir C:/HTTP/htdocs/] RewriteCond: input='/abc//b//' pattern='^//+(.*)$' => not-matched
127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (4) [perdir C:/HTTP/htdocs/] RewriteCond: input='/abc//b//' pattern='^(.*/)/+$' => matched
127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (2) [perdir C:/HTTP/htdocs/] rewrite 'abc/b/' -> 'http://domain//abc//b/'
127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (2) [perdir C:/HTTP/htdocs/] explicitly forcing redirect with http://domain//abc//b/
127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (1) [perdir C:/HTTP/htdocs/] escaping http://domain//abc//b/ for redirect
127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (1) [perdir C:/HTTP/htdocs/] redirect to http://domain//abc//b/ [REDIRECT/301]

注意:请考虑不要对主机进行硬编码:

Note: consider not hard-coding the host:

RewriteCond %{HTTP_HOST} !=""
RewriteCond %{REQUEST_URI} ^//+(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*/)/+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]

还要注意,内部的"//"没有被替换.您将添加另一个规则来替换内部斜杠.

Also note that the inner "//" was not replaced. You will to add another rule to replace inner slashes.

新 好的,这似乎可以防止以//::

NEW OK, this seems to work for preventing URLs starting or ending with //:

RewriteEngine on
RewriteBase /
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]

这篇关于删除多个尾部斜杠mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆