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 host’s configuration. The frustration!
So how do we 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 had to install dnsmasq.
Send all .test traffic to 127.0.0.1
We’ll start with installing dnsmasq via brw.
brew install dnsmasq
After install, I had to change the config of dnsmasq a bit by adding:
# --- Added by mlasen
# 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 with some comments to keep overview.
After customising the configuration, I had to add a resolver;sudo mkdir /etc/resolver
sudo nano /etc/resolver/test
And add the following to the test
file created above:nameserver 127.0.0.1
Then it’s time to start the dnsmasq 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 was using NGINX, so I edited the NGINX configuration file via File -> Edit Template -> nginx.conf and added the following part to the server_name parameter for both HTTP and HTTPS websites:~^(.*).MAMP_VirtualHost_ServerName_MAMP$;
So, for HTTPMAMP_VirtualHost_ServerName_MAMP
is replaced byMAMP_VirtualHost_ServerName_MAMP ~^(.*).MAMP_VirtualHost_ServerName_MAMP$
And for HTTPSMAMP_SSLVirtualHost_ServerName_MAMP
is replaced byMAMP_SSLVirtualHost_ServerName_MAMP ~^(.*).MAMP_SSLVirtualHost_ServerName_MAMP$
Done! MAMP now handles all .test traffic and NGINX is configured to handle subdomains as well! Byebye subdomains aliases!
Leave a Reply