我在重定向 URL 已更改的頁面時遇到問題,但我希望將舊 URL 重定向到新 URL。這是.htaccess檔案
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. )$ index.php?/$1 [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
在最后一行中使用 mod_rewrite 我沒有被重定向到新的 URL,但是當我使用 mod_alias(Redirect) 規則時,我被重定向到新的 URL,但舊的 URL 以這種格式附加到其中。
https://example.com/contact-us/?/contactus/
我已經在 Stack Overflow 上搜索了這個問題的解決方案,并嘗試了許多類似的場景,但沒有奏效。
uj5u.com熱心網友回復:
像這樣給定的順序。
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^contactus/$ /contact-us/ [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/system [NC]
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. )$ index.php?/$1 [L]
RewriteEngine On 在同一個 .htaccess 中不需要多次。
在測驗更改之前,請確保清除瀏覽器快取。
uj5u.com熱心網友回復:
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://example.com/$1 [R,L] RewriteEngine On RewriteCond %{HTTP_HOST} ^www.example.com [NC] RewriteRule ^(.*)$ https://example.com/$1 [L,R=301] RewriteEngine On RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
您的 mod_rewrite 外部重定向(最后三個規則)位于錯誤的位置。這些需要在內部重寫之前(前端控制器)。通過將它們放在檔案的末尾,它們永遠不會被除靜態資源之外的任何 URL 處理,因為所有其他 URL 都被路由到index.php,此時處理停止。
當我使用 mod_alias(Redirect) 規則時,我被重定向到新 URL,但舊 URL 被附加
無論組態檔中指令的順序如何,不同的模塊都會獨立處理。mod_alias在mod_rewrite之后被處理- 但它被處理。但是,該Redirect指令是前綴匹配,匹配之后的所有內容都附加到目標 URL 的末尾。
并且無需重復RewriteEngine指令
有這樣的指令:
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/system
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. )$ index.php?/$1 [L]
并確保在測驗前清除瀏覽器快取。最好使用 302(臨時)重定向進行測驗以避免快取問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357806.html
上一篇:使用php在pdf中搜索文本
