apache 从子域重定向到域 + .htaccess

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1321123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:05:46  来源:igfitidea点击:

redirect from subdomain to domain + .htaccess

apache.htaccessmod-rewrite

提问by earl

Is is possible that i can configure RewriteRule for more then one domain.

我可以为多个域配置 RewriteRule 是可能的。

Like my requirement is My current domain name www.maindomain.com and let say i have three domian and subdomain the subdomain url is example1.maindomain.com example2.maindomain.com example3.maindomain.com

就像我的要求是我当前的域名 www.maindomain.com 假设我有三个域和子域,子域 url 是 example1.maindomain.com example2.maindomain.com example3.maindomain.com

Now i want when ever user try to access www.example1.com it should get the content of example1.maindomain.com and the same for example2, example3

现在我希望当用户尝试访问 www.example1.com 时,它应该获取 example1.maindomain.com 的内容,对于 example2、example3 也是如此

I am using apache + passenger.

我正在使用 apache + 乘客。

Thanks for help.

感谢帮助。

回答by earl

First, write a condition that matches all the domain names you want to redirect. Using the matched part of the domain, write a rule that rewrites to the target subdomain URLs. So, given the desired mapping stated in your question, something like the following should do the trick:

首先,编写一个与您要重定向的所有域名匹配的条件。使用域的匹配部分,编写重写目标子域 URL 的规则。因此,考虑到您的问题中所述的所需映射,以下内容应该可以解决问题:

RewriteCond %{HTTP_HOST} ^www\.(example[123])\.com$ [NC]
RewriteRule ^(.*) http://%1.maindomain.com/ [L,R]

The above rewrites from e.g. www.example1.comto example1.maindomain.com. Similarly, if you need it the other way round:

上面从 eg 重写www.example1.comexample1.maindomain.com. 同样,如果您反过来需要它:

RewriteCond %{HTTP_HOST} ^(example[123])\.maindomain\.com$ [NC]
RewriteRule ^(.*) http://www.%1.com/ [L,R]

This would rewrite e.g. example2.maindomain.comto www.example2.com.

这将重写例如example2.maindomain.comwww.example2.com.