如何使用Apache将两个域重定向到另一个域?

时间:2013-01-09 02:28:17

标签: apache .htaccess mod-rewrite

我有三个域,其中两个应该被重定向到另一个域。

www.example.com
www.example.net
www.example.org

我已经设置了DNS条目,以便它们都将转到相同的IP地址。

我想要的是将.com和.net网址永久重定向到.org地址。所以:

http://www.example.com - > http://www.example.org
http://www.example.net - > http://www.example.org
http://example.com - > http://www.example.org
http://example.net - > http://www.example.org

在我的.htaccess文件中,我有以下配置,我根据对http://httpd.apache.org/docs/2.2/rewrite/remapping.html#canonicalhost的最佳理解设置了

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*)$ http://www.example.org/$1 [L,R=301]

理论上,应该发生的是对HTTP_HOST www.example.org的网站的任何请求,然后它应该被永久地重定向到http://www.example.org/,然后是任何原始路径这是在URL上。

我确信这很容易做到,我只是错过了一些明显的东西,但似乎所有其他问题和搜索结果都谈到了重定向子域和文件路径,但没有一个谈论重定向顶部URL中的级别域。

2 个答案:

答案 0 :(得分:1)

这与我使用的几乎相同:

RewriteEngine on
RewriteCond  %{HTTP_HOST}   !^www.example.org$
RewriteRule ^/(.*)$ http://www.example.org/$1 [R=301,L]

答案 1 :(得分:0)

原来我走在正确的轨道上。我最后的代码就是这样:

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

问题的根源在于我的主人不知道我有多个域名。因此,当请求在解决后进入网站时,主机会抛出一个页面,说它找不到网站。所以我将我的example.com和example.net网站添加到我的主机并将它们停放到example.org。

也许其他人可以比我更好地解释这里发生的事情,但真正的问题不在于重写,而在于我的托管服务提供商。