簡體   English   中英

.htaccess 從域重定向到另一個域

[英].htaccess redirect from domain to another

我可以將模式host.com/* 的所有請求重定向到otherhost.com/*

例如所有對host.com/page1 的請求都需要重寫為otherhost.com/page1

困難的部分是不應重寫對host.com本身(主頁)的請求。

這就是我試圖做的
RewriteRule ^(.*)$ http://otherhost.com/$1 [R=301,L]
但不斷重寫包括主頁在內的所有請求

非常感謝任何幫助

您應該添加一些條件:

RewriteEngine on
RewriteCond %{REQUEST_URI} !=/
RewriteCond %{REQUEST_URI} !^/index.html
RewriteRule ^(.*)$ http://otherhost.com/$1 [R=301,L]

您可以使用以下重寫:

RewriteEngine on
RewriteCond %{HTTP_HOST} =host.com
RewriteCond %{REQUEST_URI} !=/
RewriteRule . http://otherhost.com%{REQUEST_URI} [R=301,NE,L]

RewriteCond %{HTTP_HOST} =host.com檢查主機是否為host.com

RewriteCond %{REQUEST_URI} !=/檢查請求的頁面是否不是“索引”。 (如果這就是您所說的“不應重寫對 host.com 本身(主頁)的請求”的意思)

R=301表示它應該在重定向時發送HTTP 301 Moved Permanently標頭。

NE聲明它不應轉義特殊字符。 (例如?轉義到%3f
這個很重要! 如果不指定這個,很多 URL 都會出錯。 (例如/blah.php?id=1將被重定向到/blah.php%3fid%3d1 ,這是錯誤的。)

試試這個:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^(/index.html)?$
RewriteRule (.*)  http://otherhost.com/$1  [R=301,L]

暫無
暫無

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

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