Multiple LocalHost Sites

I’ve got XAMPP installed on a Windows 7 machine. I wanted a way to test multiple sites locally.

Set up the local host file

In your host file, add (replace with the domains you want):

127.0.0.1    sub.domain.com.dev
127.0.0.1    www.example.com.dev
127.0.0.1    www.unfocus.com.dev

UPDATE: Thanks to xip.io the host file edits are optional if you use the style www.example.com.127.0.0.1.xip.io (replace 127.0.0.1 with your actual IP address). This is compatible with Adobe’s Edge Inspect.

Set up XAMPP config files

In \xampp\apache\conf\httpd.conf uncomment the following at about line 140:

LoadModule vhost_alias_module modules/mod_vhost_alias.so

I’ve used the folder structure of “\xampp\htdocs\www.example.com\web\content\files.php” (for example) and the following config will find the folder with the exact name in the url.

In \xampp\apache\conf\extra\httpd-vhosts.conf:

NameVirtualHost *:80

<virtualhost *:80>
    DocumentRoot C:/xampp/htdocs/
</virtualhost>

# New: No hosts file edits if you use this style
# Replace X.X.X.X with your local IP
<virtualhost X.X.X.X:80>
    # Handle Edge Code IPs
    VirtualDocumentRoot D:/xampp/htdocs/websites/%-7+/web/content/
    ServerAlias *.X.X.X.X.xip.io
    php_admin_value auto_prepend_file D:/xampp/setdocroot.php
</virtualhost>

<virtualhost *:80>
    VirtualDocumentRoot C:/xampp/htdocs/%-2+/web/content/
    ServerAlias *.dev
    php_admin_value auto_prepend_file C:/xampp/setdocroot.php
</virtualhost>

Fix DOCUMENT_ROOT

If you are having issues with DOCUMENT_ROOT, create setdocroot.php at “\xampp\” and put the following in it:

<?php  $_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'],"",$_SERVER['SCRIPT_FILENAME']);  ?>

References:
https://issues.apache.org/bugzilla/show_bug.cgi?id=26052#c27
http://stackoverflow.com/questions/138162/wildcards-in-a-hosts-file
http://postpostmodern.com/instructional/a-smarter-mamp/

2 thoughts on “Multiple LocalHost Sites”

Leave a Reply

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

Connect with Facebook

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>