Marinus Klasen

Marinus Klasen

  • Development
  • Consultancy
  • Developer Resources
  • Blog
  • Contact

October 13, 2020

Wildcard subdomains in MAMP Pro on MacOS

Marinus Klasen

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 HTTP
MAMP_VirtualHost_ServerName_MAMP
is replaced by
MAMP_VirtualHost_ServerName_MAMP ~^(.*).MAMP_VirtualHost_ServerName_MAMP$

And for HTTPS
MAMP_SSLVirtualHost_ServerName_MAMP
is replaced by
MAMP_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!

About Marinus Klasen

Marinus has been working in software/web development for more than a decade. Since 2020 his attention shifted on sharing knowledge and developing products and tools for sharing knowledge.

Marinus Klasen on Twitter

This site runs on Cloudways.
It's fast isn't it?
Cloudways offers high-quality, fast and affordable hosting.

Learn more

Need a hand? Post your project and hire me and other top notch developers on Codeable.

Hire me on Codeable

Comments

  1. Moch says

    January 24, 2022 at 12:05 pm

    Thank you MKlasen,

    What would be the equivalent for Apache, MacOS and Mamp Pro?

    Reply
    • Marinus Klasen says

      January 26, 2022 at 10:51 am

      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

      Reply
  2. Moch says

    January 26, 2022 at 1:52 pm

    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.

    Reply
    • Marinus Klasen says

      January 26, 2022 at 2:11 pm

      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.

      Reply

Leave a Reply Cancel reply

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

Ready to take action?

I'm looking forward to discuss your projects and goals!
Feel free to reach out using the contact details below.

Marinus Klasen

[email protected]
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Storing private data with SSH on WPEngine sitesDecember 15, 2022
  • Get the HTML content of a block edit page or post in WordPressNovember 30, 2022
  • Rename Coupon code text to Discount code in WoocommerceNovember 3, 2022
  • Background-size cover in mPDFOctober 22, 2022
  • WordPress.com SSH & duplicating a WordPress.com website locallyOctober 11, 2022

Copyright © 2023 · Marinus Klasen | Webdesign by Team Rood