【问题标题】:How to map subdomain to subfolder automatically如何自动将子域映射到子文件夹
【发布时间】:2015-02-24 15:45:09
【问题描述】:

我正在使用一个 PHP 脚本,该脚本在每个用户注册时为他创建一个子文件夹。例如:domain.com/users/user1

我需要将子域映射到这些子文件夹,例如:user1.domain.com.

我正在使用 XAMPP,我关注了this tutorial,它运行良好。

但我必须为每个用户/子域执行此操作!

我需要在用户注册时自动执行此操作。

【问题讨论】:

  • 为什么要为每个用户设置一个子域?目的是什么?虚拟主机?
  • @LucasF 是的,虚拟主机
  • 这个问题应该发到ServerFault...

标签: php xampp virtualhost


【解决方案1】:

我一直在考虑这个问题,但从未测试过。 我认为您应该尝试与某些 MVC 框架(例如 CodeIgniter)相同的方法

使用 index.php 路由所有请求。 获取 $_SERVER["HTTP_HOST"] 并解析它以获取您的子域。 现在根据您的子域加载相应的视图(MVC 中的视图)

【讨论】:

    【解决方案2】:

    您想在 Apache 上进行大规模虚拟主机。在这里您将找到如何执行此操作的信息:

    Dynamically configured mass virtual hosting

    根据您链接的教程中的示例:

    NameVirtualHost *
      <VirtualHost *>
        DocumentRoot "C:\xampp\htdocs"
        ServerName localhost
      </VirtualHost>
      <VirtualHost *>
        DocumentRoot "C:\Documents and Settings\Me\My Documents\clientA\website"
        ServerName clientA.local
      <Directory "C:\Documents and Settings\Me\My Documents\clientA\website">
        Order allow,deny
        Allow from all
      </Directory>
    </VirtualHost>
    <VirtualHost *>
        DocumentRoot "C:\Documents and Settings\Me\My Documents\clientB\website"
        ServerName clientB.local
      <Directory "C:\Documents and Settings\Me\My Documents\clientB\website">
        Order allow,deny
        Allow from all
      </Directory>
    </VirtualHost>
    

    关于此配置的问题是,它是静态的,更改时需要重新启动 Apache。

    首先,您需要在 DNS 服务器中创建一条记录,以便将所有子域映射到您的服务器。像这样:

    *.local. 3600 IN A x.x.x.x
    

    要在 localhost 上进行测试,您可以在 hosts 文件中手动设置一些子域。请参阅 here 为什么无法在 hosts 文件中设置通配符子域。

    别忘了在 httpd.conf 中加载 vhost_alias_module:

    LoadModule vhost_alias_module modules/mod_vhost_alias.so
    

    然后你将替换你的虚拟主机配置,就像这个例子:

    <VirtualHost *>
        # get the server name from the Host: header
        UseCanonicalName Off
    
        # this log format can be split per-virtual-host based on the first field
        LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
        CustomLog logs/access_log vcommon
    
        # include the server name in the filenames used to satisfy requests
        VirtualDocumentRoot "C:/Documents and Settings/Me/My Documents/%1/website"
    
        <Directory "C:/Documents and Settings/Me/My Documents/*/website">
            Options Indexes FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
            Require all granted
            DirectoryIndex  index.php index.html index.htm
        </Directory>
    </VirtualHost>
    

    这将使用通配符来设置请求的子域的文档根目录。

    【讨论】:

    • 我知道这可能需要做很多工作,但是尝试将文档中的至少一个基本示例内联到您的答案中,以防万一链接发生变化(希望不会发生,因为它是 apache2 文档,但是谁知道),所以有一些具体的东西,将来其他人可以具体评论。
    • @LucasF 我以前读过这个主题,但我不能完全理解它或将它应用到我的网站上。你能在这里简化一下吗?
    【解决方案3】:

    您可以安装web hosting control panel (如PleskWebmin)来为您完成这项工作。您将使用友好的 GUI 来配置子域,所有细节配置都将在后台进行。这就是现实世界的托管服务提供商所使用的。

    【讨论】:

      【解决方案4】:

      我通过使用服务器上的默认虚拟主机实现了类似的功能。假设你有你的 dns 设置,以便所有子域都指向适当的 IP 地址,那么它们应该都解析为默认的 vhost。默认虚拟主机指向的文件夹也需要有一个 index.php 文件,该文件利用子域来提供适当的内容。

      您可以通过编辑 /etc/hosts 文件并将本地 dns 中的子域指向本地主机来使用 XAMPP 在本地复制此内容。将您的 webroot 设置为 index.php 文件所在的位置,并从 $_SERVER 变量中获取域名。从那里您可以通过子域确定用户并以编程方式显示内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-30
        • 2012-07-16
        • 1970-01-01
        • 1970-01-01
        • 2015-08-06
        • 1970-01-01
        • 2017-07-07
        相关资源
        最近更新 更多