繁体   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