将所有子域重定向到新的根域

Redirect all subdomains to a new root domain

我想更改站点的根域。

相关域有多个子域作为别名。

我想使用 htaccess 将子域重定向到新域:

例如:

test.domain1.com > test.domain2.com

环顾四周,我需要的条件如下:

RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com$ [NC]

但是我看不出 RewriteRule 应该是什么。

如有任何帮助,我们将不胜感激。

要将 test.subdomain.com 重定向到 test.subdomain2.com,您可以使用以下规则:

RewriteCond %{HTTP_HOST} ^([^.]+)\.domain1\.com$ [NC]
RewriteRule ^ http://%1.domin2.com%{REQUEST_URI} [NE,L,R]

这是正确的方法:

Remember: Replace example.com, and example\.com below, with your own domain name.

对于 HTTPS:// 协议:

RewriteCond %{HTTP_HOST} ^(([^.]+)\.)?example\.com$ [NC]
RewriteRule ^ https://%1example.com%{REQUEST_URI} [NE,R=301,L]

对于 HTTP:// 协议:

RewriteCond %{HTTP_HOST} ^(([^.]+)\.)?example\.com$ [NC]
RewriteRule ^ http://%1example.com%{REQUEST_URI} [NE,R=301,L]

@starkeen 的回答没有考虑 根本没有子域 的域(例如,简单地说,'https://example.com')。