21

On our LAN I've set up several 'fake' TLDs in the DNS server, with the intention of using them for Apache name-based virtual hosting. I'd like to combine this with mass-virtual-hosting (i.e. VirtualDocumentRoot) on an Ubuntu 10.04 LAMP server.

However, I can't get it to select the right vhost!

Here is a summary of the Apache config:

NameVirtualHost 10.10.0.205

<VirtualHost 10.10.0.205>
   ServerName *.test
   VirtualDocumentRoot /var/www/%-3.0.%-2/test/%1/
   CustomLog /var/log/apache2/access.log vhost_combined
</VirtualHost>

<VirtualHost 10.10.0.205>
   ServerName *.dev
   VirtualDocumentRoot /var/www/%-3.0.%-2/dev/%1/
   CustomLog /var/log/apache2/access.log vhost_combined
</VirtualHost>

A hostname such as www.domain.com.dev, correctly resolves to 10.10.0.205, but always selects the top vhost, instead of the bottom one, which matches more closely.

I was under the impression that Apache would first try to match the ServerName before defaulting to the top vhost for a given IP. What am I doing wrong? Or is this not possible and must I use another IP for each TLD?

apachectl -S outputs (trimmed):

10.10.0.205:*          is a NameVirtualHost
    default server *.test
    port * namevhost *.test
    port * namevhost *.dev

2 Answers 2

49

Use ServerAlias, rather than ServerName alone:

ServerName somename.dev

ServerAlias *.dev

2
  • 4
    Recent apache versions will no longer even allow wildcards in ServerName, and the error tells you to use ServerAlias instead. Oct 1, 2015 at 18:04
  • Awesome. This was quick and to the point. Def saved me time. Thanks.
    – Art Geigel
    Jul 27, 2016 at 0:38
4

If this can help, just to complete a bit Mo's answer:

Wildcards in ServerAlias also works on partial domain names, such as

ServerName somename.dev

ServerAlias *-mysite.dev

It saved my life recently ^^ (a main site with subdomains for each sub app such as admin-somename.dev, bo-somename.dev, api-somename.dev...)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .