簡體   English   中英

.htaccess 重定向動態 url

[英].htaccess redirect dynamic url

我有這個.htaccess來重定向具有 URL 中特定關鍵字的 URL:

RewriteEngine on

RewriteRule ^testa/(.*)$ https://app.domain2.com/testa/$1 [R=301,NC]
RewriteRule ^testb/(.*)$ https://app.domain2.com/testb/$1 [R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]  

I want to redirect if the URL contains /testa/ or /testb/ to a new domain URL: https://app.domain2.com/testa/$1 and https://app.domain2.com/testb/$1 respectively.

我有這個 URL https://app.domain1.com/testa/page/1應該重定向到https://app.domain2.com/testa/page/1

使用上面的.htaccess代碼我沒有得到重定向結果。 而且我想僅使用特定參數名稱將 URL 重定向,而不是將所有 URL 從 domain1 重定向到 domain2。

 RewriteRule ^testa/(.*)$ https://app.domain2.com/testa/$1 [R=301,NC] RewriteRule ^testb/(.*)$ https://app.domain2.com/testb/$1 [R=301,NC]

您在兩個重定向上缺少L ( last ) 標志。 因此處理繼續進行,最后一條規則最終將請求重寫為index.php?/並帶有格式錯誤的 301 HTTP 狀態 - 由於Location Z099FB995346F31C749F6E40DB0F395E 不存在,因此不會發生重定向。

您需要在這兩個規則/重定向中包含L標志,以便立即觸發外部重定向。 IE。 [R=301,NC,L]


在旁邊:

如果您要重定向到目標站點上的相同 URL 路徑(例如testatesta ),那么您不妨捕獲整個 URL 路徑,而不僅僅是testa (或testb )之后的部分。 如果它只是testatestb那么你只需要一個使用正則表達式交替的規則。 例如:

RewriteRule ^(testa|testb)/.* https://app.domain2.com/testa/$0 [R=301,NC,L]

$0反向引用包含與RewriteRule模式匹配的整個 URL 路徑。

注意:首先使用 302(臨時)重定向進行測試,以避免潛在的緩存問題。

暫無
暫無

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

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