的.htaccess - 重定向子域名和路径相同的子域名和路径上的另一个域 [英] .htaccess - Redirect subdomain and path to same subdomain and path on another domain

查看:176
本文介绍了的.htaccess - 重定向子域名和路径相同的子域名和路径上的另一个域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过远播的解决方案,但一直没能找到一个解决我的问题。

I have looked far and wide for a solution but haven't been able to find one that resolved my issue.

我想实现301重定向,将用户重定向到另一个域中,使用相同的子域名和路径,因为他们要求的人。

I am trying to implement a 301 redirect that would redirect the user to another domain, with the same subdomain and path as the one they requested.

即。如果他们的土地上sub1.domain1.com/page.php他们重定向到sub1.domain2.com/page.php~~V。重定向应该抓住任何子域。

i.e. if they land on sub1.domain1.com/page.php they get redirected to sub1.domain2.com/page.php. The redirect should catch ANY subdomain.

我已经设置了的AllowOverride所有的在/ var / www / html等目录,并尝试了以下,但没有运气:

I have set the AllowOverride to All on the /var/www/html directory and tried the following but with no luck:

#RewriteEngine On
#RewriteCond %{HTTP_HOST} (.*).domain1.com
#RewriteRule (.*) http://%1.domain2.com/$1 [R=301,QSA,L]

任何帮助很多AP preciated。在此先感谢!

Any help is much appreciated. Thanks in advance!

推荐答案

作为评论W3D提到的,标志需要从每一行的开头删除为了使规则生效。

As w3d mentioned in a comment, the # signs need to be removed from the beginning of each line in order for the rules to take effect.

此外,你应该更新这样的:

Also, you should update this:

RewriteCond %{HTTP_HOST} (.*).domain1.com

...所以它现在看起来是这样的:

... so that it now looks something like this:

RewriteCond %{HTTP_HOST} ^(.+)\.domain1\.com$

^ 符号表示一个值的开头,而 $ 表示一个值的结尾。

The ^ sign signifies the beginning of a value, and the $ signifies the end of a value.

原因更换(。*)(+)是因为理论上你告诉你服务器 .domain1.com 是一个有效的域名,当您使用(。*)(而事实上应该有至少是一次第一个小数点前一个字符)。

The reason for replacing (.*) with (.+) is because theoretically you're telling your server that .domain1.com is a valid domain name when you use (.*) (when in fact there should at least be once character before the first dot).

的原因反斜线是逃避里面的的RewriteCond 的点。请参见此页面关于重写条件和规则的详细信息。

The reason for the backslashes is to escape the dots inside the RewriteCond. See this page for more info on rewrite conditions and rules.

最终你可能会像这样结束:

Ultimately you might end up with something like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.domain1\.com$
RewriteRule ^(.*)$ http://%1.domain2.com/$1 [R=301,QSA,L]

这篇关于的.htaccess - 重定向子域名和路径相同的子域名和路径上的另一个域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆