我看到了類似的主題,但找不到對我的問題的實用答案。
我正在將舊網站移至新網站,并且一些 URL 正在更改。
我想對新域進行通用 301 重定向(因為大多數路徑都相同),同時單獨重定向一些 URL。
這是我在舊網站 .htaccess 上的內容:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule (.*)$ https://new.com/$1 [R=301,L]
Redirect 301 "/custom/url/" "https://new.com/my-custom-url"
</IfModule>
但是 301 重定向到:https : //new.com/custom/url而不是https://new.com/my-custom-url
我的一些 URL 也有我想重定向的 URL 引數,例如:
Redirect 301 "/brand.php?name=Example" "https://new.com/Example"
Redirect 301 "/brand.php?name=Example2" "https://new.com/another/url"
這似乎也不起作用。
非常感謝您的幫助。
uj5u.com熱心網友回復:
但是 301 重定向到 :
https://new.com/custom/url而不是https://new.com/my-custom-url
這是因為您的特定重定向規則出現在通用規則之后。此外,您將 mod_rewrite 規則與 mod_alias 規則混合在一起,并且這些規則在不同時間被呼叫。
像這樣:
RewriteEngine On
# redirect /brand.php?name=Example2 to new.com/another/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example2) [NC]
RewriteRule ^brand\.php$ https://new.com/another/%1? [R=301,L,NE]
# redirect /brand.php?name=Example3 to new.com/category/Example3
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example3) [NC]
RewriteRule ^brand\.php$ https://new.com/category/%1? [R=301,L,NE]
# generic redirect /brand.php?name=Example to new.com/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=([^&] ) [NC]
RewriteRule ^brand\.php$ https://new.com/%1? [R=301,L,NE]
# redirect custom URL
RewriteRule ^custom/url/ https://new.com/my-custom-url [R=301,L,NE,NC]
# redirect everything else
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^ https://new.com%{REQUEST_URI} [R=301,L]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/364346.html
