通配符子域和静态子域的虚拟主机

我有一个奇怪的情况,我希望有网址 app1.example.comexample.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>

问题是它们之间有冲突。谁先被列出来谁就赢了。如何同时托管通配符虚拟主机和特定的虚拟主机?

注意: 我不仅仅是在配置中更改 DocumentRoot,所以使用 mod_rewrite更改 DocumentRoot 变量并不能修复它。

126494 次浏览
<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.

Wildcards can only be used in the ServerAlias rather than the ServerName. Something which had me stumped.

For your use case, the following should suffice

<VirtualHost *:80>
ServerAlias *.example.com
VirtualDocumentRoot /var/www/%1/
</VirtualHost>

This also works for https needed a solution to making project directories this was it. because chrome doesn't like non ssl anymore used free ssl. Notice: My Web Server is Wamp64 on Windows 10 so I wouldn't use this config because of variables unless your using wamp.

<VirtualHost *:443>
ServerAdmin test@test.com
ServerName test.com
ServerAlias *.test.com


SSLEngine On
SSLCertificateFile "conf/key/certificatecom.crt"
SSLCertificateKeyFile "conf/key/privatecom.key"


VirtualDocumentRoot "${INSTALL_DIR}/www/subdomains/%1/"


DocumentRoot "${INSTALL_DIR}/www/subdomains"
<Directory "${INSTALL_DIR}/www/subdomains/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>