I spent a good hour or so trying to get wildcard VirtualHosts working on my Mac - here I’m going to outline the steps I took, as information on the web was very sparse.
I wanted to point *.dev.vxnick.com to my local development machine (my Mac) using Dynamic DNS and MAMP’s version of Apache.
It turned out that
/etc/hosts doesn’t allow wildcard entries, so I enabled BIND (
named) but struggled to find adequate information for setting up a wildcard domain other than localhost (which would’ve been easier).
After much Googling, I just decided to add each subdomain of dev.vxnick.com to
/etc/hosts, as follows:
127.0.0.1 example1.dev.vxnick.com 127.0.0.1 example2.dev.vxnick.com
This worked out well as I only had a few to enter. I didn’t bother with the www prefix as I don’t use that for development, but if you want to use it then you would need to add it above with the non-www versions.
Once the DNS entries were in
/etc/hosts I flushed the DNS cache just for good measure - I did this with
dscacheutil -flushcache through Terminal.app.
This is where things got frustrating - I opened MAMP’s Apache configuration file (
/Applications/MAMP/conf/apache/httpd.conf) and started to fiddle around with the VirtualHost settings.
Let me just mention how I was testing this, as this was pretty crucial to making sure it was setup correctly - I had two VirtualHosts setup - one for example1.dev.vxnick.com and the other to act as a catch-all for non-matches. Note: the default (catch-all) VirtualHost needs to be the first listed.
Now, the DocumentRoot for example1 was pointed at
/Users/nick/Sites/example1 with the default (catch-all) VirtualHost’s DocumentRoot pointing at
/Users/nick/Sites - matching the default DocumentRoot right at the top of
Here’s where the problems started - I tested browsing to example1.dev.vxnick.com and plain old localhost on my Mac. For some reason, example1 wasn’t being matched to its corresponding VirtualHost, so I was getting the default index file for that URL, which obviously was incorrect.
I was also testing with a cURL script on this server - it would establish a connection to example1.dev.vxnick.com and then output whatever HTML it got from Apache. I had similar problems with this in that it wouldn’t match its corresponding VirtualHost and show me the correct directory contents.
Anyway, after much editing and restarting of Apache, I stumbled upon the following setup for the VirtualHosts. I don’t know whether this works with more than two, as that’s all I tested with.
NameVirtualHost *:80 <VirtualHost *:80> ServerName localhost DocumentRoot /Users/nick/Sites </VirtualHost> <VirtualHost *:80> ServerName example1.dev.vxnick.com ServerAlias www.example1.dev.vxnick.com DocumentRoot /Users/nick/Sites/example1 </VirtualHost>
To be honest, I’m not sure what it is that fixed the problem, as I was changing and restarting so often I wasn’t really keeping track.
Hopefully this saves someone else having to go through the pain of getting this working.
Needless to say, I can’t be held responsible if this breaks your configuration, destroys your hard drive or really, really irritates you.