簡體   English   中英

使用新 URL 重定向 301 附加舊 URL

[英]Redirect 301 appending old URL with New URL

我在重定向 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 上搜索了這個問題的解決方案,並嘗試了許多類似的場景,但沒有奏效。

像這樣給定的順序。

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]

在同一個 .htaccess 中不需要多次RewriteEngine On

在測試更改之前,請確保清除瀏覽器緩存。

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_aliasmod_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(臨時)重定向進行測試以避免緩存問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM