Configuring subdomains in development with


I do a lot of work with subdomains, managing them locally on my development and test machines has always been a bit of a pain. Going way back, I can remember manually editing the hosts file in system32\drivers\etc\ on Windows! On the Unix we have /etc/hosts.

Unfortunately /etc/hosts doesn’t allow wildcard definitions for subdomains, so you have to manually specify each one. For a while I used this simple bash script to append a new domain (or subdomain) to the /etc/hosts file;



if [ $# -lt 1 ]
  echo "which subdomain?"
  if [ -n "$SUBDOMAIN" ]

if [ $# -lt 2 ]
  echo "which tld?"
  read TLD

echo " $SUBDOMAIN$TLD" >> /etc/hosts
echo "OK, added $SUBDOMAIN$TLD to /etc/hosts"

Save this script as executable somewhere in your PATH, call it something like addhost then run it from anywhere like so;

> addhost # will prompt for subdomain, tld (leave blank for no subdomain)
> addhost www

This soon became tiresome, with some apps requiring 10’s or 100’s of subdomains to be tested and available locally. So I took to writing a simple rake task that would automatically pull subdomain names from an applications database and append them to my local /etc/hosts file (here’s that script).

Time passed and my script worked well, but had a some disadvantages. I had to re-run it every-time my subdomain data changed, and across apps it required modified since I had different ways and means of defining subdomain sites.

Enter the most excellent suggestion from Tim Pope. Using DNS to solve this problem means no more hassle! He set up a localhost wildcard, pointing * at Essentially this means that [anything] will point to your local machine. Not happy with does the same thing (lvh, as in local virtual host). Or since most developers have a couple of old domains lying dormant, why not use one for this? I learned of Tim’s technique through the Subdomains in Rails 3 screen-cast from Ryan Bates.

So I think that wraps up all my posts on subdomains, whats that? four now!? Here are the other three.

January 10, 2011 17:54 by

3 comments so far

Leave a comment