Subdomains for localhost

A handy trick I just learned: you can use your hosts file to create subdomains of localhost.

For example, I have WordPress and Drupal both running on my machine for local web development testing. I used to access one at http://localhost/wp and the other at http://localhost/drupal. This works just fine, in general, but can lead to some awkwardness with things like .htaccess files and relative links when I move the site I’m developing to a real server.

By editing the hosts file, though, I can access my development sites at http://wp.localhost/ and http://drupal.localhost/, making it a little easier to test, migrate, etc.

In Windows (XP and Vista), you can find your hosts file in the C:\Windows\System32\drivers\etc directory. Add a couple of lines to the end of the file:

127.0.0.1       wp.localhost
127.0.0.1       drupal.localhost

And create virtual hosts in your Apache httpd.conf to point those domains to the correct directories. E.g.:

<VirtualHost *:80>
    DocumentRoot D:\xampp\htdocs\drupal
    ServerName drupal.localhost
</VirtualHost>