我知道这个问题被问了一些关于独此网站的时间,但通过相关的帖子浏览我无法找到一个解决方案。试图域后删除多个尾随斜线。以下mod_rewrite的表情似乎工作的网址,如 HTTP:// WWW。 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]
有帮助吗?

解决方案

不能再现。域后,立即额外的斜杠从来没有通过,即使关闭DirectorySlashes到mod_rewrite的 - 我没有检查它是否是歌剧或Apache多数民众赞成去掉斜线)。但除此之外,一切正常:

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

请求 HTTP:// //本地主机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]

注意:考虑不是硬编码在主机:

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

另外请注意,内部的“//”未被取代。您将添加另一条规则来取代内部斜杠。

新的编辑: OK,这似乎为防止网址的开始或结束与//工作:

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]

其他提示

你告诉阿帕奇地图的一切,包括第一斜线$ 1

RewriteRule ^(.*)$ domain/$1 [R=301]

RewriteRule (.*) domain/$1? [R=301,L]

RewriteRule ^(.+)/$ $1 [R=301,L]

添加斜线插入符之后或域名后丢弃所述一个

下面是这似乎是工作完整列表:

DirectorySlash Off
RewriteEngine on

# Fix Canonical URLs
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 %{HTTP_HOST} !=""
RewriteCond %{query_string} q= [NC]
RewriteRule (.*) http://%{HTTP_HOST}/$1? [R=301,L]

# Remove multiple slashes after domain
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]

# 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 and ads
RewriteCond %{REQUEST_URI} !^/ads/
RewriteRule ^(.+)/$ $1 [R=301,L]

这一个发送重定向之前除去所有的斜线

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

在下面的代码将去除所有多余的斜线包括域后多余的斜线。

RewriteCond %{THE_REQUEST} //
RewriteRule ^(.*)$ /$1 [L,R=301]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top