Local Domain Without Hosts File on OSX

Where I do a lot of local development I need to set up a lot of virtual hosts etc for each new project. I’m always looking for ways to shave a bit of time off this process and make it quicker and simpler to set up. In general all of my projects follow a similar pattern. I assign a 3 letter code to the project and use this to set up a local domain, always in the form of xxx.localhost. This convention in general makes things very quick for me but I still end up having to edit a few files to set up a new site.

One thing that has bugged me for a while is having to edit my hosts file to add a new entry. In DNS world I would have a wildcard domain *.localhost which mapped to my machine, however this isn’t available in the hosts file so I ended up having to manually add it each time and my hosts file contained hundreds of entries.

Today I deleted all those entries and instead set up dnsmasq to create a *.localhost situation. It was fairly straightforward, only bit slightly difficult was the need to add to the list of DNS servers. I use Macports, but Homebrew is just as straightforward.

  • sudo port install dnsmasq
  • vim /opt/localhost/etc/dnsmasq.conf
  • add “address=/localhost/” to the end of the file
  • sudo port load dnsmasq
  • Open up Network preferences, add to the DNS Servers List
  • ping xxx.localhost – should return

That’s it. A wildcard DNS for *.localhost set up and working. One less file to remember to edit.

Leave a comment

Your email address will not be published. Required fields are marked *