我正在使用一个 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/