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:nameserver 127.0.0.1
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:~^(.*).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 subdomain aliases!
Moch says
Thank you MKlasen,
What would be the equivalent for Apache, MacOS and Mamp Pro?
Marinus Klasen says
Hey Moch,
It’s been a while since I’ve used MAMP Pro. I bumped into this Stackoverflow post while researching your question:
https://stackoverflow.com/a/31166699/2695951
When I open MAMP and look at httpd.conf via File -> Open Template -> Apache -> httpd.conf I notice the lines below around line number 746:
MAMP_VirtualHost_iteration_begin_MAMP
ServerName MAMP_VirtualHost_ServerName_MAMP
MAMP_VirtualHost_ServerAdmin_MAMP
MAMP_VirtualHost_DirectoryIndex_MAMP
DocumentRoot “MAMP_VirtualHost_DocumentRoot_MAMP”
I think adding `ServerAlias *.test` should do the job for you. Let me know if this works for you and I’ll update the article!
– Marinus
Moch says
Hi Marinus,
Thank you. I have tried and didn’t work either. Also, another issue I have had to face, is that Mamp Pro generates SSL certs that don’t work for wildcard subdomains.
Marinus Klasen says
Hmm too bad. I can’t help you any further with this as I’ve fully switched to docker containers. For certificates; I use mkcert (https://github.com/FiloSottile/mkcert) to generate (wildcard) certificates for local development.