Apache Sub-Subdomains and existing wildcard Subdomain

apache-2.2subdomainwildcard-subdomain

So I have an existing wildcard DNS A record, and subdomain virtual server setup as follows

DocumentRoot /path/to/code
<Directory "/path/to/code">
allow from all
Options +Indexes
</Directory>

ServerName existing.domain.co.uk
ServerAlias *.domain.co.uk

…I now want to add another virtual server, which works in the same manner, but with a sub-subdomain, to allow me to access a branch of the codebase from something like monkeys.alt.domain.co.uk

DocumentRoot /path/to/altcode
<Directory "/path/to/altcode">
allow from all
Options +Indexes
</Directory>

ServerName alt.domain.co.uk
ServerAlias *.alt.domain.co.uk

In the DNS zonefile I have added another A record from *.alt to the IP (which happens to be the same IP as just the * A record).

Now what I think is happening is that the existing wildcard is picking up monkeys.alt.domain.co.uk and sending it to the existing virtual server rather than the new one.

My Question: How do I get the traffic from monkeys.alt.domain.co.uk to goto the new 'alt' virtual server?

Best Answer

Apache works with the virtual hosts in the same order as it reads their configuration. So if he reads first the ServerAlias *.domain.co.uk then any sub or sub.sub.domain will match. If Apache can read first ServerAlias *.alt.domain.co.uk then any subdomain of alt.domain.co.uk will match but not the existing.domain.co.uk, which will match the next vHost.

So if you have both virtual hosts described in the same file, place the second (the monkey.alt vHost) before the main vHost. If they are in separate files, rename the file so the monkey vHost config file will be read first (add a 0 at the begin, for example).

Related Topic