88

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).

<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.

3 Answers 3

179
<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.

Sign up to request clarification or add additional context in comments.

11 Comments

Just a question, what does NameVirtualHost *:80 do?
This directive enables the name based virtual hosts and will tell apache to listen on all ip's (*) on port 80. Apache 2.2 Docs: httpd.apache.org/docs/2.2/en/vhosts/name-based.html
Remove the NameVirtualHost *:80: AH00548: NameVirtualHost has no effect and will be removed in the next release
The wild card one does not work for me, do you know why?
NameVirtualHost *:80 is important for apache 2.2, no need for apache 2.4, I don't know why.
|
33

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> 

1 Comment

This is a great solution but for future reference does require a2enmod vhost_alias for the vhost_alias module to be turned on.
2

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 [email protected] 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> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.