【发布时间】: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