使虚拟主机检测带有和不带有前面 www 的通配符

使虚拟主机检测带有和不带有前面 www 的通配符

在我的 Apache(Xampp)httpd-vhosts.conf文件中,我添加了这个虚拟主机,它允许我使用通配符名称testserver1.devtestserver2.dev我只需要确保将名称添加到我的 Windows Hosts 文件中。

<VirtualHost *:80>
  VirtualDocumentRoot  E:/Server/htdocs/projects/%1/www
  ServerAlias *.dev
</VirtualHost>

不过,我想做的是添加此功能,并使其在名称以wwwso开头时testserver1.dev也能正常工作。www.testserver1.dev

按照目前的设置方式,如果我尝试访问该 URL,它将查找名为的文件夹,www.testserver1而不是文件夹testserver1

答案1

删除 ServerAlias 并将 %1 更改为 %-2。

<VirtualHost *:80>
  VirtualDocumentRoot  E:/Server/htdocs/projects/%-2/www
</VirtualHost>

正如mod_vhost_alias 文档的目录名称插值部分,这%-2将告诉 Apache 查看名称的倒数第二部分。

链接文档的下一部分有示例,其中之一如下:

VirtualDocumentRoot "/usr/local/apache/vhosts/%-2.0.%-1.0"

这将允许www.example.com以及www.sub.example.comexample.com提供来自的文件/usr/local/apache/vhosts/example.com。它执行的操作如下:

%-2.0  # The second from the last part of the name with a zero-length substring.
       # The .0 is necessary as we need to specify a literal period next and don't
       # want the parser to confuse it with a substring specification.

.      # a literal period

%-1.0  # the last part of the name with a zero-length substring. The .0 should be
       # optional here as there's nothing left to cause confusion, but it doesn't
       # hurt anything to have it.

相关内容