简体   繁体   English

通过 .htaccess 从 URL 中删除双斜杠不起作用

[英]Removing Double Slashes From URL By .htaccess does not work

How come that none of these solutions work on my Apache servers:为什么这些解决方案都不能在我的 Apache 服务器上运行:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*) $1 [R=302,L]

or或者

RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=302,L]

or或者

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=302,L]

among others that I tried.在我尝试过的其他人中。

I tried ALL solutions from this page: Issue In Removing Double Or More Slashes From URL By .htaccess我尝试了此页面中的所有解决方案: .htaccess 从 URL 中删除双或更多斜杠时出现问题

and other pages as well.以及其他页面。

The problem is that the rule in the htaccess does not match the double slashes within these above patterns.问题是 htaccess 中的规则与上述模式中的双斜杠不匹配。

I tried also "literal" patterns, with exact urls without regex patterns.我也尝试了“文字”模式,没有正则表达式模式的精确网址。 Still nothing.依然没有。 But with a single slash - all work.但是用一个斜线 - 一切正常。

It seems like Apache has a problem when it finds: "//" - the url is clearly not recognized and the rule is ommited. Apache 发现时似乎有问题:“//” - 显然无法识别 url 并且省略了规则。

The idea is simple: to get rid of double slashes and replace them with one slash:这个想法很简单:去掉双斜线并用一个斜线替换它们:

 http://demo.codesamplez.com/html5//audio -> http://demo.codesamplez.com/html5/audio

Do you know how can I redirect URL with double slash "//" to a single onen "/"?您知道如何将带有双斜杠“//”的 URL 重定向到单个“/”吗?

Here's htaccess (removed the longest comments in the file):这是 htaccess(删除了文件中最长的注释):

<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On


RewriteCond %{REQUEST_URI} ^/test//slash
RewriteRule ^(.*)$ /test/slash [R=302,L]


RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Rewrite all other queries to the front controller.
RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>

Try the following instead, near the top of the root .htaccess file, before any existing rewrites:在任何现有的重写之前,请在根.htaccess文件的顶部附近尝试以下操作:

# Remove multiple slashes anywhere in the URL-path
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule (.*) /$1 [R=302,L]

This uses the fact that multiple slashes have already been reduced in the URL-path matched by the RewriteRule pattern .这利用了在与RewriteRule模式匹配的 URL 路径中已经减少了多个斜杠的事实。 And the check against THE_REQUEST (which contains the first line of the request headers and does not change throughout the request ) ensures that multiple slashes were initially present somewhere in the URL-path (excluding the query string).并且对THE_REQUEST的检查(它包含请求标头的第一行并且在整个请求中不会更改)确保多个斜杠最初出现在 URL 路径中的某处(不包括查询字符串)。

Another potential issue is if you have a proxy server (or load balancer) in front of your application (Apache) server and this is perhaps normalizing the request (reducing multiple slashes, removing trailing space, etc) as it forwards the request to your application (Apache) server.另一个潜在问题是,如果您的应用程序 (Apache) 服务器前面有一个代理服务器(或负载平衡器),这可能是在将请求转发到您的应用程序时规范化请求(减少多个斜杠、删除尾随空格等) (阿帕奇)服务器。 The application server then never sees the original request (with multiple slashes) that you see in the browser.然后,应用程序服务器永远不会看到您在浏览器中看到的原始请求(带有多个斜杠)。


Looking at your attempts...看着你的尝试...

 RewriteCond %{REQUEST_URI} ^/test//slash RewriteRule ^(.*)$ /test/slash [R=302,L]

This "should" work, with the limited example as posted.这个“应该”工作,发布的例子有限。 However, the REQUEST_URI server variable is modified throughout the request, so if the URL has already been modified (perhaps in the server config) then this may not match.但是, REQUEST_URI服务器变量在整个请求过程中都会被修改,因此如果 URL 已经被修改(可能在服务器配置中),那么这可能不匹配。

 RewriteCond %{THE_REQUEST} ^[AZ]{3,}\s/{2,} [NC] RewriteRule ^(.*) $1 [R=302,L]

This only matches multiple slashes at the start of the URL-path, not anywhere in the URL-path.这仅匹配 URL 路径开头的多个斜杠,而不是 URL 路径中的任何位置。 This would also result in a malformed redirect if used in .htaccess (unless you also had a RewriteBase directive set).如果在.htaccess中使用,这也会导致格式错误的重定向(除非您还设置了RewriteBase指令)。 Without the slash prefix on the substitution string this rule is probably intended for a server or virtualhost context, not .htaccess .如果替换字符串上没有斜杠前缀,则此规则可能适用于服务器虚拟主机上下文,而不是.htaccess

 RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$ RewriteRule . %1/%2 [R=302,L]

The same issue with the use of REQUEST_URI as mentioned above.与上面提到的使用REQUEST_URI相同的问题。 Otherwise, this should work.否则,这应该有效。 However, it results in multiple redirects if there are more than 1 group of multiple slashes.但是,如果有超过 1 组的多个斜杠,则会导致多次重定向。 eg.例如。 //foo//bar . //foo//bar .

 RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ RewriteRule . %1/%2 [R=302,L]

The same as above, except this only matches double slashes, rather than groups of two or more slashes.与上面相同,除了这仅匹配双斜杠,而不是两个或多个斜杠的组。 So if there are more than two slashes in a group it will result in multiple redirects.因此,如果一个组中有两个以上的斜杠,则会导致多个重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM