2012-04-18 77 views
0

我拥有两个域名:虚拟主机设置多个域的名称和www版本重定向到一个非www域名

  • domain.com
  • domain.net

,我还可以决定如何最佳设置虚拟代码以实现以下几点:

  1. 如果访问者输入这两个域名中的任意一个, Ødomain.net
  2. 如果要么在前面WWW这两个域名的访问者类型,他们仍然domain.net加www
  3. 守在原地文件路径,这样:
    • domain.com/file.php最终要domain.net/file.php
    • www.domain.com/deeper/path/最终要domain.net/deeper/path/
    • www.domain.net最终要domain.net
    • ...等等

我看到,在this Q/A entry,我相信我需要什么,但我不能100%确定。有人可以确认(或纠正),这将做我所需要的:

<VirtualHost *:80> 
    ServerName domain.net 
    ServerAlias www.domain.net domain.com www.domain.com 
    (the rest of the settings here) 
</VirtualHost> 

......是这么简单吗?

回答

3

现在,您的虚拟主机会接受您的别名并以正确的文件响应,但不会重定向请求。如果你想有一个规范的子域,您可能需要添加一些重写规则:

<VirtualHost *:80> 
    ServerName domain.net 
    ServerAlias www.domain.net domain.com www.domain.com 

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

    (the rest of the settings here) 
</VirtualHost> 

检查http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html#canonicalhost进一步解释

+0

谢谢主席先生!我会在这个周末尝试一下,让你知道它对我的影响。 – 2012-04-19 13:53:07

+0

终于有了一个自由的时刻来尝试一下,它是完美的。再次感谢! – 2012-04-24 03:25:36

相关问题