我正在尝试为我的虚拟主机创建后备。我的配置如下所示:
# Fetch all pre-defined hosts
Include "conf/extra/vhosts/*.conf"
# Fallback
NameVirtualHost *:80
<Directory "C:/LocalServer/usr">
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
VirtualDocumentRoot "C:/LocalServer/usr/%-1/projects/%-2+/public/"
</VirtualHost>
这里的目标如下:如果我尝试访问 http://test.lab/
,我想让它自动拾取以下目录:C:/LocalServer/usr/lab/projects/test/public/
.现在,我已经创建了文件夹和一个空的索引文件(
index.php
)。尽管如此,Apache 一直向我显示一个空的目录索引(“Index of”)。不太确定现在要做什么。尝试了一些东西,但似乎都不起作用。
有任何想法吗?
更新 - 6 月 1 日
我现在正在使用这个代码,基于第一个答案(好吧,唯一的一个):
<VirtualHost *:80>
UseCanonicalName Off
ServerAlias *.lab
VirtualDocumentRoot "C:/LocalServer/%2/%1/public"
<Directory "C:/LocalServer/%2/%1/public">
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
我现在得到一个 禁止访问 来自 Apache 的错误。当目录不存在时,通常肯定会收到此错误? C:/LocalServer/lab/test/public
确实存在,并且是空的 index.php
驻留在public
目录。一般错误日志中的错误:
[client 127.0.0.1:49342] AH01797: client denied by server configuration: C:/LocalServer/lab/test/public/
如果我删除 <Directory/>
组,没有任何变化。我仍然收到错误。 (我什至可以在该组中使用 %n
吗?)快速说明:
之前它不工作的原因是因为我通过
Include "conf/extra/vhosts/*.conf"
导入了其他虚拟主机。操作说明。将其注释掉(从而使实验室规则成为唯一的规则)发起了 禁止访问 错误。另请注意,我不再使用
usr
文件夹 - 每个实验室现在都在 lab
中文件夹,在 LocalServer
下.更新 2
看来
<Directory/>
块不允许插入变量,例如 VirtualDocumentRoot
做。更新 3 - 找到解决方案
它现在正在工作 - 没有帮助就无法做到。这是最终的代码:
<VirtualHost lab:80>
UseCanonicalName Off
ServerAlias *.lab
VirtualDocumentRoot "C:/LocalServer/%2/%1/public"
<Directory "C:/LocalServer/lab/*/public">
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
更新 4(2015 年 4 月)新指令,对于那些感兴趣的人(使用最新的 Apache 2.4):
<VirtualHost *:80>
UseCanonicalName Off
ServerAlias *.local
VirtualDocumentRoot "D:/home/%-2+/public_html"
<Directory "D:/home/*/public_html">
Require all granted
AllowOverride All
Options Indexes FollowSymLinks
</Directory>
</VirtualHost>
这与Acrylic DNS Proxy的组合,变魔术。更新 5(2016 年 12 月)
我现在正在使用 Macro approach .
# Directory Macro - Default Directory configuration on a per-vhost basis
<Macro Directory $dir>
<Directory "z:/var/www/$dir/public_html">
Require all granted
Options Includes Indexes FollowSymLinks
AllowOverride All
</Directory>
</Macro>
# LocalSub Macro - For specific *.*.local subs that require their own root
<Macro LocalSub $sub $domain>
<VirtualHost 127.0.0.1>
ServerName $sub.$domain.local
DocumentRoot “z:/var/www/$domain/$sub/public_html”
Use Directory $domain/$sub
</VirtualHost>
</Macro>
Use LocalSub blog rockettpw
# Main virtual host
<VirtualHost 127.0.0.1>
UseCanonicalName Off
ServerAlias *.local *.*.local
VirtualDocumentRoot “z:/var/www/%-2/public_html”
Use Directory *
</VirtualHost>
最佳答案
我使用它们 :) 您忘记关闭规范名称 - 不幸的是,我不知道为什么我的配置中必须有 ServerAlias - 没有它它就无法工作 - 下面的代码已经过测试并且可以正常工作
<Directory "C:/LocalServer/*/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require local
</Directory>
<VirtualHost *:80>
# Apache will form URLs using the hostname supplied by the client
UseCanonicalName Off
# available aliases to use
ServerAlias *.lab *.lab2
# where to put them
VirtualDocumentRoot "C:/LocalServer/%2/%1/public/"
</VirtualHost>
关于apache2 - 带有通配符 VirtualDocumentRoot 的 VirtualHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16747013/