【问题标题】:Removing Double Slashes From URL By .htaccess does not work通过 .htaccess 从 URL 中删除双斜杠不起作用
【发布时间】:2021-11-26 20:04:12
【问题描述】:

为什么这些解决方案都不能在我的 Apache 服务器上运行:

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

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

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

我尝试过的其他人。

我尝试了此页面中的所有解决方案:Issue In Removing Double Or More Slashes From URL By .htaccess

以及其他页面。

问题是 htaccess 中的规则与上述模式中的双斜杠不匹配。

我还尝试了“文字”模式,使用没有正则表达式模式的精确网址。依然没有。但只需一个斜线 - 一切正常。

Apache 发现好像有问题:"//" - url 明显无法识别,规则被省略。

这个想法很简单:去掉双斜线并用一个斜线替换它们:

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

您知道如何将带有双斜杠“//”的 URL 重定向到单个“/”吗?

这里是 htaccess(删除了文件中最长的 cmets):

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

【问题讨论】:

  • 您的.htaccess 文件中还有其他指令吗?请附上您完整的.htaccess 文件。
  • 我将 .htacccess 包含在已编辑的问题中,只是删除了长 cmets。这是非常简单和标准的文件,但是我也在另一台服务器上尝试过,但是 .htaccess 太大了,我决定保持简单 - 上述规则不适用于这些服务器。如您所见,即使使用简单的文字模式,它也不起作用: RewriteCond %{REQUEST_URI} ^/test//slash
  • 您使用的是什么版本的 Apache?您的应用程序 (Apache) 服务器前面是否有代理服务器/负载平衡器?

标签: .htaccess url apache2 slash


【解决方案1】:

请尝试以下方法:

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

这利用了RewriteRule模式匹配的 URL 路径中多个斜杠已经减少的事实。并且检查THE_REQUEST(它包含请求标头的第一行并且在整个请求中不会改变)确保多个斜杠最初出现在 URL 路径中的某处(不包括查询字符串)。

另一个潜在问题是,如果您的应用程序 (Apache) 服务器前面有一个代理服务器(或负载平衡器),这可能会在转发请求时规范化请求(减少多个斜杠、删除尾随空格等)到您的应用程序 (Apache) 服务器。然后,应用程序服务器永远不会看到您在浏览器中看到的原始请求(带有多个斜杠)。


看看你的尝试......

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

这个“应该”工作,发布的例子有限。但是,REQUEST_URI 服务器变量在整个请求过程中都会被修改,因此如果 URL 已经被修改(可能在服务器配置中),那么这可能不匹配。

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

这仅匹配 URL 路径的 start 处的多个斜杠,而不匹配 URL 路径中的任何位置。如果在.htaccess 中使用,这也会导致格式错误的重定向(除非您还设置了RewriteBase 指令)。如果 substitution 字符串上没有斜杠前缀,则此规则可能适用于 servervirtualhost 上下文,而不是 .htaccess

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

与上面提到的REQUEST_URI 使用相同的问题。否则,这应该有效。但是,如果有超过 1 组的多个斜杠,则会导致多次重定向。例如。 //foo//bar.

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

与上面相同,除了这仅匹配双斜杠,而不是两个或多个斜杠的组。因此,如果一个组中有两个以上的斜线,则会导致多次重定向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-12
    • 2013-06-09
    • 2013-04-14
    • 2019-09-03
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多