為什么這些解決方案都不適用于我的 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]
其中包括我嘗試過的。
我嘗試了此頁面中的所有解決方案:通過 .htaccess 從 URL 中洗掉雙重或更多斜杠的問題
以及其他頁面。
問題是 htaccess 中的規則與上述模式中的雙斜杠不匹配。
我還嘗試了“文字”模式,使用沒有正則運算式模式的確切網址。依然沒有。但是用一個斜線 - 所有的作業。
Apache 發現問題時似乎有問題:“//” - 顯然無法識別 url 并且忽略了規則。
這個想法很簡單:去掉雙斜線并用一個斜線替換它們:
http://demo.codesamplez.com/html5//audio -> http://demo.codesamplez.com/html5/audio
您知道如何將帶有雙斜杠“//”的 URL 重定向到單個“/”嗎?
這是 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>
uj5u.com熱心網友回復:
請嘗試以下操作:
# 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 路徑開頭的多個斜杠,而不匹配URL 路徑中的任何位置。如果在 中使用,這也會導致格式錯誤的重定向.htaccess(除非您也有一個RewriteBase指令集)。如果替換字串上沒有斜杠前綴,此規則可能適用于服務器或虛擬主機背景關系,而不是.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]
同上,除了這僅匹配雙斜線,而不是兩個或多個斜線的組。因此,如果一個組中有兩個以上的斜杠,則會導致多次重定向。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318225.html
