Have you ever wished you could add domains on your local machine (127.0.0.1) on the fly and not have to modify your /etc/hosts file?
These are the steps that will allow you to create foo.localhost, bar.localhost, myproject.localhost …. just anything.localhost
Why .localhost? Back in 1999 localhost top level domain (tld) was reserved by the Internet Engineering Task Force (IETF), see RFC 2606. They also reserved test tld (.test) if you are interested in separating test from development.
How do we accomplish this?
$ /usr/bin/ruby -e "$(curl -fsSL <a rel="noreferrer noopener" href="https://raw.githubusercontent.com/Homebrew/install/master/install" target="_blank">https://raw.githubusercontent.com/Homebrew/install/master/install</a>)"
brew install dnsmasq
Create config folder if it doesn’t already exist
mkdir -pv $(brew —- prefix)/etc/
Configure dnsmasq for *.localhost
echo 'address=/.localhost/127.0.0.1' >> $(brew -- prefix)/etc/dnsmasq.conf
Configure the port for macOS High Sierra
echo 'port=53' >> $(brew —- prefix)/etc/dnsmasq.conf
Start dnsmasq as a service so it automatically starts at login
sudo brew services start dnsmasq
Create a dns resolver
sudo mkdir -v /etc/resolversudo bash -c '<mark>echo</mark> "nameserver 127.0.0.1" > /etc/resolver/localhost'
Verify that all .localhost requests are using 127.0.0.1
You should see something like this
resolver #8domain : localhostnameserver : 127.0.0.1flags : Request A records, Request AAAA recordsreach : 0x00030002 (Reachable,Local Address,Directly Reachable Address)
You can also test by using ping
ping foo.localhost<br>ping anything.localhost
Now you can reference *.localhost and it will point to your local development machine. Now Profit!