A while back I was debugging the front-end creation of multisites in WordPress. On each install, a new subdomain was created which forced me to add a new alias to the website configuration. The frustration!
So how do you solve that?
MacOS does not allow you to add a wildcard subdomain to your
/etc/hosts file so I had to come up with another solution. After a bit of research I found that I could use dnsmasq.
Send all .test traffic to 127.0.0.1
Let’s start with installing dnsmasq via brew.
brew install dnsmasq
After install, change the config of dnsmasq by running
nano $(brew --prefix)/etc/dnsmasq.conf and adding:
# --- Added by mklasen # Route all *.test addresses to localhost address=/test/127.0.0.1 # Don't read /etc/resolv.conf or any other configuration files. no-resolv # Never forward plain names (without a dot or domain part) domain-needed # Never forward addresses in the non-routed address spaces. bogus-priv # ----
I added this on top of the file.
After customising the configuration, add a resolver;
sudo mkdir /etc/resolver
sudo nano /etc/resolver/test
And add the following to the
test file created above:
Ready to start the service?
sudo brew services start dnsmasq
Done! All .test traffic is now handled by our localhost.
Make MAMP listen to the subdomains, without having to set aliases.
Finally, MAMP also has to be aware of these changes. In this case I used an NGINX configuration, so I edited the config file via File -> Edit Template -> nginx.conf and added the following part to the server_name parameter for both HTTP and HTTPS websites:
So, for HTTP
is replaced by
And for HTTPS
is replaced by
Done! MAMP now handles all .test traffic and NGINX is configured to handle subdomains as well! Byebye subdomain aliases!