Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am developing a site, which will make use of any given [variation of] subdomain name part (that is, the part prefixed before the host name and, optionally, the TLD part). I would imagine that in production, that would be an easy feat - make sure the DNS for second-level domain name part points to an IP, set up Apache2 virtual host to listen on that (or any) IP port 80, and just use PHP to make decisions based on the "Host" request header.

However, currently the site is localhost, since I am developing it using my workstation, so first I patched the /etc/hosts to include: mydomain

I only used one name part (arguably a custom TLD) so as to not interfere with the Internet domain names. Then I set up a VirtualHost directive for Apache 2.2 like:

<VirtualHost *:80>
    ServerName mydomain

But now I can see that f.e. example.mydomain does not point to localhost, meaning the the /etc/hosts addition is not effective for "something.mydomain". It appears the rules are taken verbatim, and also I have checked that wildcards like *.mydomain are not allowed.

Is there a solution for this?

share|improve this question
up vote 5 down vote accepted

If you are on Linux (and I assume you are, since you mentioned /etc/hosts), you can try dnsmasq for this. I use it at times for quick testing and local DNS overrides.

Steps required:

  1. Install dnsmasq
  2. Configure your station to use dnsmasq for DNS resolution.

    Edit /etc/resolv.conf and put:


    The side effect of this is local DNS caching, which normally is another plus (though be aware of this).

  3. Modify dnsmasq configuration.

    Edit /etc/dnsmasq.conf:


    You need to put the root domain here, it should resolve all subdomains to localhost this way.

  4. Restart dnsmasq

  5. Verify with host and host Both should be pointing to localhost.

share|improve this answer
Thanks Karol. That did the trick. HOWEVER, requests for resolving "mydomain" (without any prefix) don't work without also specifying "mydomain" in /etc/hosts, which suggests that dnsmasq interprets the syntax as applicable to *.mydomain - i.e. only with a subdomain name part prefixing the dot and the rest. – amn Apr 23 '10 at 19:16
By my side it works without /etc/hosts modifications. Setting redirects * and too. But maybe there's some limitation here about TLDs, I don't know. Are you using literal mydomain? ;-) Maybe mydomain.localnet would work? – Karol Piczak Apr 23 '10 at 19:30
@user41339 - the correct response to a good answer that fixes your problem is to check the checkmark beside the vote count. An upvote is also a good present if you haven't done so. – Paul Tomblin Apr 23 '10 at 19:43
I know, I know :-) – amn Apr 30 '10 at 11:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.