« Back to blog

Blog: Wildcard Subdomains for quick Virtual Host creation

Graham Weldon

28 May, 2009

Being a developer of web applications, you are probably working on a number of sites at once. Its really handy to have them placed on their own subdomain or domain in order to provide separation. Linux systems will allow you to create a wildcard subdomain by placing a wildcard into the hosts file.

Since changing to OSX this is something I have missed. Fortunately, tonight I found a solution. It allows you to use as many domains as you like without changes to your hosts file. Just setup your apache virtual hosts, and off you go! Productivity++

First things first. We're actually going to create a fake top level domain (TLD) and allow wildcarding off that TLD to create our limitless domains. We do this by configuring and launching the named server builtin to OSX.

Most of this required superuser access. While I don't recommend doing this often if at all, its handy to get an elevated bash prompt:

$ sudo bash

To begin, generate an rndc.conf file and associated key. This is as simple as copying + pasting the commands below:

# rndc-confgen > /etc/rndc.conf
# head -n 6 /etc/rndc.conf > /etc/rndc.key

I don't like messing with localhost, so I plan to create the following domain wildcard possibilities. One is for general development (Mostly CakePHP, but this an be for any name resolution at all), and the other for CakeDC development:


With this in mind, create two zone files for the corresponding domains at /var/named/cdc.zone and /var/named/dev.zone and populate with the following zone information. Note that the only change you need to make is to ensure the $ORIGIN line reflects the domain for the current zone.

$TTL    3600
$ORIGIN dev.
@ 1D IN SOA   @ root (
    2009052801 ; serial
    3H         ; refresh
    15M        ; retry
    1W         ; expiry
    1D )       ; minimum

  1D IN NS    @
  1D IN A

Next, edit the /etc/named.conf file to add the following. Again, look where you see 'dev' below, and change for each TLD you are setting up.

zone "dev" IN {
    type master;
    file "dev.zone";
    allow-update { none; };

Are we there yet? Very nearly. Yes.

Edit the file /System/Library/LaunchDaemons/org.isc.named.plist and modify the <true/> node to read <false/> underneath the Disabled key: <key>Disabled</key>

Now, lets get this happening.

Issue the following, and ignore any errors you get on the first two commands.

# launchctl stop org.isc.named
# launchctl unload /System/Library/LaunchDaemons/org.isc.named.plist
# launchctl load /System/Library/LaunchDaemons/org.isc.named.plist
# launchctl start org.isc.named

The next thing to do is either modify your network settings in System Preferences to ensure is used as a DNS server before any others, or to edit /etc/resolv.conf to add as a nameserver at the top of the list.

You should now be able to resolve any *.dev and *.cdc domains.

What are the benefits? Well, you get a nice name on its own private locally accessible domain to use for development, and you require any extra work to add a new one, since we're using a wildcard. Simply setup a new name virtual host in apache (or your selected favourite web server) and have it use any subdomain of your new TLDs.

If you are working on a larger business network and need internal domains like this for teams to view, you can do the same thing, just modifying the address for the A record in each of the zone files. This even works for public internet servers.

A little known secret is that my web server hosts a number of "hidden domains" that only a select few people have access to for development and staging purposes.

I hope thats useful to someone out there. If you have any way to improve this, let me know.