Virtualhost For Wildcard Subdomain and Static Subdomain

Dave picture Dave · Apr 17, 2009 · Viewed 112k times · Source

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.

Answer

Tim picture Tim · Apr 17, 2009
<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.