我正在使用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
要在localhost上对此进行测试,您可以手动在hosts文件中设置一些子域.请参见here为什么无法在hosts文件中设置通配符子域.
不要忘记在httpd.conf中加载vhost_alias_module:
LoadModule vhost_alias_module modules/mod_vhost_alias.so
然后你会替换你的vhost配置,就像这个例子:
<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>
这将使用通配符来设置所请求的子域的文档根目录.