Mass VHOST wildcard; subdomain to folder

I couldn’t find any information as my search terms were incorrect. My google-fu was off when I was searching.
My objective was to make http://* go to /var/www/*/www, where * is a wildcard.
I’m hoping that this will help others; specifically developed that work on multiple projects.
First, I wanted to wildcard my in my hosts file. I knew I couldn’t do this: *

as that’s now how hosts files work.
DNSMASQ to the rescue, to wildcard the hosts file.
Apache mass vhost, is a technique to setup lots of vhosts based on a pattern.
Some applications such as magento likes to have a proper full domain name.  Also makes testing more realistic as well.
Since I’m on OSX, install it via ports.
The instructions are a mash of: and other sites… you may have to restart dnsmasq after you’ve made the changes.

sudo port install dnsmasq
sudo cp /opt/local/etc/dnsmasq.conf /etc/dnsmasq.conf
sudo mkdir -p /System/Library/StartupItems/DNSMASQ
3.) sudo mkdir -p /System/Library/StartupItems/DNSMASQ
4.) sudo nano /System/Library/StartupItems/DNSMASQ/DNSMASQ
. /etc/rc.common
if [ "${DNSMASQ}" = "-YES-" ]; then
  ConsoleMessage "Starting DNSMASQ"
5.) sudo nano /System/Library/StartupItems/DNSMASQ/Startup Parameters.plist
  Description = "Local DNSMASQ Server";
  Provides = ("DNS Masq");
  OrderPreference = "None";
  Messages =
  start = "Starting DNSMASQ";
  stop = "Stopping DNSMASQ";
6.) Add line to /etc/hostconfig
7.) sudo chmod +x /System/Library/StartupItems/DNSMASQ/DNSMASQ
8.) sudo /System/Library/StartupItems/DNSMASQ/DNSMASQ
9.) In network settings make sure is in your dns server list.  If you do:
  You should see something like this near the bottom (if not, your network settings need to
  be applied.. try this to flush dns cache: dscacheutil -flushcache ):
    ;; SERVER:
  # test it.
  : PING ( 56 data bytes
  : 64 bytes from icmp_seq=0 ttl=64 time=0.031 ms

add the line to /etc/dnsmasq.conf
$> sudo nano -w /etc/dnsmasq.conf ## or your favourite editor, as root.


Clear your dns cache.

dscacheutil -flushcache

You may need to setup your network connection to point your DNS to localhost first.
sudo kill -HUP `cat /opt/local/var/run/`
my vhosts Magic

Alias /phpmyadmin /opt/local/www/phpmyadmin
<directory "/www">
	Options -Indexes FollowSymLinks
	AllowOverride AuthConfig FileInfo
	Order allow,deny
	Allow from all
<directory "/opt/local/www/phpmyadmin">
	Options -Indexes FollowSymLinks
	AllowOverride AuthConfig FileInfo
	Order allow,deny
	Allow from all
UseCanonicalName Off
LogFormat "%V %h %l %u %t "%r" %s %b" vcommon
CustomLog logs/access_log vcommon
# include part of the server name in the filenames
VirtualDocumentRoot /www/hosts/%1/www

Leave a Reply

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