php - 如何自动将子域映射到子文件夹

标签 php xampp virtualhost

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

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

我正在使用 XAMPP,并且已关注 this tutorial效果很好。

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

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

最佳答案

您想在 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

要在本地主机上进行测试,您可以在 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>

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

关于php - 如何自动将子域映射到子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27672216/

相关文章:

php - 无法向数据库插入新行

ubuntu - 如何设置本地服务器进行开发?

apache - 使用 SSL 时 VirtualHost 配置(重写)不起作用

php - 通过 PHP(树莓派)运行 C 程序

php - 将 i18n 添加到 Web 应用程序的最佳方法是什么?

mysql - 如何降级XAMPP中的phpmyadmin

Apache IP 虚拟主机

apache - 访问 Magento 安装时出现 302 响应

php - 如何在 1 个横幅中显示 div - opencart

mysql - 将 mysql 数据库从以前的 xampp 复制到新的 xampp