Last Updated: February 25, 2016
·
3K
· joecritch

Automatic virtual hosts for Apache

Automatic virtual host entries. (Works well with Dnsmasq, to essentially create an automatic /etc/hosts file.)

So, this code will take the name of a directory in your 'Sites' folder, and append a .dev address automatically.

Note: This will override port 80 for all sites. (So, presuming that you use different ports for other platforms, and you don't use Pow.cx, you'll be fine.)

Note 2: Don't forget to change your username ;)

<Virtualhost *:80>
    VirtualDocumentRoot "/Users/Joe/Sites/%1"
    ServerName vhosts.dev
    ServerAlias *.dev
    UseCanonicalName Off
    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    ErrorLog "/Users/Joe/Sites/vhosts-error_log"
    <Directory "/Users/Joe/Sites/*">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all 

        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]    
    </Directory>
</Virtualhost>