I have an odd situation where I want to have the URLs app1.example.com
, example.com
and *.example.com
all using a different virtual host. This is what I have (excluding example.com
because it just makes it messier).
我有一个奇怪的情况,我想让URL app1.example.com,example.com和* .example.com都使用不同的虚拟主机。这就是我所拥有的(不包括example.com,因为它只会使它变得更加混乱)。
<VirtualHost *>
ServerName app1.example.com
ServerAlias app1.example.com
DocumentRoot = /var/www/app1
# Other configuration for this app here
</VirtualHost>
<VirtualHost *>
ServerName wildcard.example.com
ServerAlias *.example.com
DocumentRoot = /var/www/wildcard
# other configuration for this app here
</VirtualHost>
The problem is that they conflict. Whichever one is listed first wins out. How can I host both a wildcard virtualhost and a specific one?
问题是他们发生冲突。无论哪一个被列出首先胜出。如何托管通配符虚拟主机和特定主机?
Note: I'm not just changing DocumentRoot
in the config, so using mod_rewrite
to change the DocumentRoot variable does not fix it.
注意:我不只是在配置中更改DocumentRoot,因此使用mod_rewrite更改DocumentRoot变量不会修复它。
2 个解决方案
#1
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName app1.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/wildcard
ServerName other.example.com
ServerAlias *.example.com
</VirtualHost>
Should work. The first entry will become the default if you don't get an explicit match. So if you had app.otherexample.com point to it, it would be caught be app1.example.com.
应该管用。如果没有明确匹配,第一个条目将成为默认条目。因此,如果你有app.otherexample.com指向它,它将被捕获为app1.example.com。
#2
Wildcards can only be used in the ServerAlias rather than the ServerName. Something which had me stumped.
通配符只能在ServerAlias中使用,而不能在ServerName中使用。让我难过的东西。
For your use case, the following should suffice
对于您的用例,以下内容应该足够了
<VirtualHost *:80>
ServerAlias *.example.com
VirtualDocumentRoot /var/www/%1/
</VirtualHost>
there is also more information at https://www.chris-shaw.com/blog/using-wildcards-in-virtual-hosts-on-apache
还有更多信息,请访问https://www.chris-shaw.com/blog/using-wildcards-in-virtual-hosts-on-apache
#1
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName app1.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/wildcard
ServerName other.example.com
ServerAlias *.example.com
</VirtualHost>
Should work. The first entry will become the default if you don't get an explicit match. So if you had app.otherexample.com point to it, it would be caught be app1.example.com.
应该管用。如果没有明确匹配,第一个条目将成为默认条目。因此,如果你有app.otherexample.com指向它,它将被捕获为app1.example.com。
#2
Wildcards can only be used in the ServerAlias rather than the ServerName. Something which had me stumped.
通配符只能在ServerAlias中使用,而不能在ServerName中使用。让我难过的东西。
For your use case, the following should suffice
对于您的用例,以下内容应该足够了
<VirtualHost *:80>
ServerAlias *.example.com
VirtualDocumentRoot /var/www/%1/
</VirtualHost>
there is also more information at https://www.chris-shaw.com/blog/using-wildcards-in-virtual-hosts-on-apache
还有更多信息,请访问https://www.chris-shaw.com/blog/using-wildcards-in-virtual-hosts-on-apache