Sub-Domains on Windows localhost
October 25, 2008 4:18 am GeneralI find it useful when working on a web project to set up a sub-domain on my development PC's "localhost" Apache installation. This allows me to test the project by accessing pages via that sub-domain, treating the directory being used as the root of that sub-domain as if it were the web document root directory (i.e.: $_SERVER['DOCUMENT_ROOT'] in your PHP scripts will point to that directory when the page is launched via that sub-domain).
Setting up a localhost sub-domain in an Windows/Apache environment is fairly easy. You just need to set up a few lines of text in three files, then restart Apache. First, make sure that your Apache httpd.conf file has the following in it, adjusting the path to the "httpd-vhosts.conf" file as appropriate for your installation:
# Virtual hosts Include conf/extra/httpd-vhosts.conf
Then in that httpd-vhosts.conf file, add the following lines for each desired sub-domain. In this example, I'm calling my sub-domain "foobar":
<VirtualHost *:80>
ServerAdmin webmaster@foobar.localhost
DocumentRoot "C:/wamp/www/foobar"
ServerName foobar.localhost
ServerAlias www.foobar.localhost
ErrorLog "logs/foobar.localhost-error.log"
CustomLog "logs/foobar.localhost-access.log" common
</VirtualHost>
Lastly, you need to add the sub-domain to your Windows hosts file, normally located at C:\Windows\System32\drivers\etc\hosts:
127.0.0.1 foobar.localhost
Now all that's left is to restart Apache, then point your browser to "http://foobar.localhost/".

Felix :
Date: October 26, 2008 @ 16:57
How to do the same on Ubuntu (but should work on other Debian-based distros):
1. Run this command:
sudo nano /etc/apache2/sites-available/foobarNote: you can use any text editor for this. I chose nano because it’s the default editor that comes with Ubuntu.
2. Type this in the file:
ServerAdmin webmaster@foobar.localhost
ServerName foobar.localhost
ServerAlias *.foobar.localhost
DocumentRoot /var/www/foobar/
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
ErrorLog /var/log/apache2/foobar.error.log
CustomLog /var/log/apache2/foobar.access.log combined
Note: The tag is needed especially if the new DocumentRoot is outside of /var/www (or the default DocumentRoot, otherwise you will get 401 Access Denied (or something like that).
3. Run:
sudo nano /etc/hostsAnd add this line:
::1 foobar.localhostNote: If this doesn’t work for you, try replacing that line with:
127.0.0.1 foobar.localhost4. Run:
sudo a2ensite foobarThis creates a symlink to the “foobar” file in the /etc/apache2/sites-available in the /etc/apache2/sites-enabled folder, this way letting Apache know you want a new site.
5. Run:
sudo /etc/init.d/apache2 reloadNote: Don’t worry if you get a warning like “apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName”, it’s ok :)
6. Go to http://foobar.localhost/ with your favorite browser and voila! :)
Note: If you never used nano, hit Ctrl+O and Enter to save a file and Ctrl+X to exit.
Have fun :)
(Yeah, I’m a Linux enthusiast :D)
Felix :
Date: October 26, 2008 @ 17:05
Bummer, it messed up my code :( here’s how the /etc/apache2/sites-available/foobar file should look like:
[VirtualHost *]
ServerAdmin webmaster@work.localhost
ServerName work.localhost
ServerAlias *.work.localhost
DocumentRoot /var/www/work/
[Directory /var/www/work/]
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
[/Directory]
ErrorLog /var/log/apache2/work.error.log
CustomLog /var/log/apache2/work.access.log combined
[/VirtualHost]
Note: Replace all ‘ [ ' with ' '.
Note: The [Directory] tag is needed especially if the new DocumentRoot is outside of /var/www (or the default DocumentRoot, otherwise you will get 401 Access Denied (or something like that).