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

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

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

我想要做的是添加到这个function,并使其工作,如果名称以www开头,所以testserver1.dev也将作为www.testserver1.dev

它目前的设置方式,如果我试图访问该URL,它会看到一个名为www.testserver1文件夹而不是文件夹testserver1

摆脱ServerAlias并将%1更改为%-2。

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

正如mod_vhost_alias文档的Directory Name Interpolation部分所述 , %-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.