首页 文章

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

提问于
浏览
-1

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

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 回答

  • 1

    这几乎与我使用的相同:

    RewriteEngine on
    RewriteCond  %{HTTP_HOST}   !^www.example.org$
    RewriteRule ^/(.*)$ http://www.example.org/$1 [R=301,L]
    
  • 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 .

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

相关问题