【问题标题】:virtual subdomain: one subdomain per user虚拟子域:每个用户一个子域
【发布时间】:2012-09-06 16:16:16
【问题描述】:

在我的网站上,我使用虚拟主机,因此我的用户可以拥有像“user1.mydomain.com”、“user2.mydomain.com”这样的虚拟域...

问题在于,在像“user1.domain.com”这样的虚拟域上,索引页面始终与我的索引页面“http://mydomain.com”上的相同。

我想要做的是为域和子域设置两个不同的索引页面。我的问题是,如何将子域重定向到“index2.php”(例如)并且仍然让子域看起来像“user1.mydomain.com”?

【问题讨论】:

  • 什么网络服务器平台...您应该能够在您的 apache http.conf 文件中为每个单独的子域指定目录(例如)。

标签: php linux subdomain virtualhost


【解决方案1】:

理想情况下,您应该为每个子域设置完全独立的文档目录。 vhost 在 apache 中是要走的路。但是,如果您想按照自己的方式进行操作(将子域重定向到单个文件),则需要做更多的工作,但仍然可行。首先,用通配符定义mod_vhost

<VirtualHost 111.22.33.55>
    DocumentRoot /www/subdomain
    ServerName www.mydomain.com
    ServerAlias *.mydomain.com

    ...
</VirtualHost>

然后在这个VirtualHost 中设置使用mod_rewrite 的重写规则:

<Location "/">
    RewriteCond %{HTTP_HOST} ^user1.mydomain.com$
    RewriteRule ^\/$ http://www.mydomain.com/index2.php [R=301,L]
    RewriteRule ^\/index.php$ http://www.mydomain.com/index2.php [R=301,L]

    RewriteCond %{HTTP_HOST} ^user2.mydomain.com$
    RewriteRule ^\/$ http://www.mydomain.com/index3.php [R=301,L]
    RewriteRule ^\/index.php$ http://www.mydomain.com/index3.php [R=301,L]

    ...
</Location>

但请注意,这仅适用于 //index.php 对子域的请求。如果您打算做更多的事情,最好为每个子域设置单独的文档根目录。

【讨论】:

    【解决方案2】:

    也许你对此感兴趣。

    http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html#virtualdocumentroot

    这允许在 Apache 服务器中自动使用基于子域的文档根目录

    【讨论】:

      【解决方案3】:

      我会使用 php 来检查 url。 如果是子域包括 index2.php 否则包括 index1.php

      在不知道你正在运行的服务器的情况下,这是我能想到的唯一方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-03
        • 2011-10-14
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-20
        相关资源
        最近更新 更多