.htaccess 如何重定向到其他域
我需要在.htaccess文件中设置一个指令以重定向,如下所示: 从 到 有人能帮忙吗 谢谢 我的代码--产生500个内部服务器错误.htaccess 如何重定向到其他域,.htaccess,redirect,.htaccess,Redirect,我需要在.htaccess文件中设置一个指令以重定向,如下所示: 从 到 有人能帮忙吗 谢谢 我的代码--产生500个内部服务器错误 //Rewrite to www Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^mydomain.com/internal[nc] RewriteRule ^(.*)$ http://myotherdomain.com/internal/$1 [r=301,nc] 试试这个:
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com/internal[nc]
RewriteRule ^(.*)$ http://myotherdomain.com/internal/$1 [r=301,nc]
试试这个:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^internal(.*)$ http://myotherdomain.com/internal$1 [L,NC]
(.*)
将复制任何内容,以便您可以在$1
中使用它。如果重定向应该是永久性的,只需在L,NC
之后添加R=301
编辑
您在给定代码中的错误有:
^mydomain.com$
[NC]
RewriteRule^(.*)$http://myotherdomain.com/$1
或重写规则^internal/(.*)$http://myotherdomain.com/internal/$1
,但您的规则将重定向到内部/内部
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^internal(.*)$ http://myotherdomain.com/internal$1 [L,NC]
(.*)
将复制任何内容,以便您可以在$1
中使用它。如果重定向应该是永久性的,只需在L,NC
之后添加R=301
编辑
您在给定代码中的错误有:
^mydomain.com$
[NC]
RewriteRule^(.*)$http://myotherdomain.com/$1
或重写规则^internal/(.*)$http://myotherdomain.com/internal/$1
,但您的规则将重定向到内部/内部
您的文件有一些问题:注释以
开头,而不是/
,无法将URI与HTTP主机匹配(您试图匹配/internal),并且规则或条件与标志(NC)之间需要有一个空格。不过,这应该是可行的:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^internal(.*)$ http://myotherdomain.com/internal$1 [R=301,NC]
您的文件有一些问题:注释以
开头,而不是/
,无法将URI与HTTP主机匹配(您试图匹配/internal),并且规则或条件与标志(NC)之间需要有一个空格。不过,这应该是可行的:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^internal(.*)$ http://myotherdomain.com/internal$1 [R=301,NC]
首先,.htaccess中的注释不是以
/
Good catch@Wooble开头的——实际上我知道它们是以磅符号(#)开头的。我从一个在线生成器那里得到了代码,但从来没有注意过这一行。首先,htaccess中的注释不是以/
Good catch@Wooble开头的——实际上我知道它们是以磅符号(#)开头的。我从一个在线生成器那里得到了代码,但从来没有注意到这一行。这行不通-你必须先重写HTTP_主机,然后才能重写域。这只是呈现了一个@ChristopherBrixThanks@ChristopherBrix的目录列表。我对安萨里的建议有足够的理解,我最终还是同意了这个建议,但我也投票支持了你的答案。@Dr.DOT你是对的-我可能会先发布,但安萨里的解决方案更好(起初我的解决方案中有一些错误)这不起作用-您必须先重写HTTP_主机,然后才能重写域。这只会呈现@ChristopherBrixThanks@ChristopherBrix的目录列表。我对安萨里的建议有足够的理解,我最终还是同意了这个建议,但我也投票支持了你的答案。@Dr.DOT你是对的-我可能会先发布,但安萨里的解决方案更好(起初我的解决方案中有一些错误),因为它值得@Ansari,.htaccess文件实际上存在于/internal/目录中,因为这是源域上我要重定向的唯一目录。结果只是给了我一个mydomain.com/internal的目录列表很高兴听到这个消息-我想建议除了RewriteBase/internal/
.Question@Ansari(和其他人)之外。我不希望在自然URL的末尾添加任何额外/多余的内容,也不希望出现任何查询字符串。因此,我可以将最后一条指令修改为RewriteRule^(.*)$http://myotherdomain.com/internal/ [R=301,NC]
?那还能用吗?是的,还能用。要防止添加查询字符串,请在末尾添加问号,如下所示:http://myotherdomain.com/internal/? [R=301,NC]
对于@Ansari来说,实际上,.htaccess文件存在于/internal/目录中,因为这是源域上我要重定向的唯一目录。结果只是给了我一个mydomain.com/internal的目录列表很高兴听到这个消息-我想建议除了RewriteBase/internal/
.Question@Ansari(和其他人)之外。我不希望在自然URL的末尾添加任何额外/多余的内容,也不希望出现任何查询字符串。因此,我可以将最后一条指令修改为RewriteRule^(.*)$http://myotherdomain.com/internal/ [R=301,NC]
?那还能用吗?是的,还能用。要防止添加查询字符串,请在末尾添加问号,如下所示:http://myotherdomain.com/internal/? [R=301,NC]