合并两个不同的域名但使用 Apache 重定向保存虚拟主机

合并两个不同的域名但使用 Apache 重定向保存虚拟主机

我有两个域名和一个有效子域名。我该如何实现?

ex-ample.com    » example.com
www.example.com » example.com
en.ex-ample.com » en.example.com
en.example.com  » en.example.com

我目前正在使用以下 .htaccess 规则:

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

尝试了很多方法,但最好的办法是重定向循环。你能帮助我吗?

答案1

如果我正确理解了您的请求,我认为典型的命名虚拟主机配置将满足您的要求。我不太清楚您所说的“保存虚拟主机”是什么意思;如果此解决方案没有帮助,也许您可​​以详细说明您的请求。

无论如何,我设想的事情是这样的:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName example.com
    ServerAlias ex-ample.com www.example.com
    DocumentRoot /path/to/example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName en.example.com
    ServerAlias en.ex-ample.com
    DocumentRoot /path/to/en.example.com
</VirtualHost>

现在请求任何...

...将转到 example.com VirtualHost,并请求...

...将转到 en.example.com VirtualHost。

答案2

如果您正在做的只是寻找重写规则,请尝试以下操作:

# REWRITE en.ex-ample.com to en.example.com
RewriteCond %{HTTP_HOST} ^en\.ex-ample\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$    
RewriteRule (.*) http://en.example.com$1 [R=301,L]

# REWRITE ANYTHING OTHER THAN 'example.com' and 'en.example.com'
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^en\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$    
RewriteRule (.*) http://example.com$1 [R=301,L]

假设您为所有适当的域都有一个虚拟主机。

相关内容