【问题标题】:vhost setup to redirect multiple domains names and www version to single non-www domainvhost 设置将多个域名和 www 版本重定向到单个非 www 域
【发布时间】:2012-04-29 12:36:07
【问题描述】:

我拥有两个域名:

  • 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>

……就这么简单吗?

【问题讨论】:

    标签: mod-rewrite vhosts


    【解决方案1】:

    按原样,您的虚拟主机将接受您的别名并使用正确的文件进行响应,但不会重定向请求。如果您想要一个规范的子域,您可能需要添加一些重写规则:

    <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了解更多说明

    【讨论】:

    • 谢谢先生!这个周末我会试试,让你知道它对我来说怎么样。
    • 终于有空来试一试,很完美。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2011-04-02
    • 2017-11-03
    • 2014-03-25
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多