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:       wp.localhost       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

4 Responses to Subdomains for localhost

  1. Yoosuf says:

    are you sure that its working fine?, for me its not working after restarting the Apache!

  2. Toxic brain says:

    it’s not working for me too

  3. Muhammad Sajid says:

    how to create subdomain of subdomain on localhost


  4. @Muhammad Sajid: You do that the same way. Just make a new entry in your hosts file and httpd.conf file for each new sub-sub-domain. Example:       example1.wp.localhost       example2.wp.localhost


    <VirtualHost *:80>
        DocumentRoot D:\xampp\htdocs\wordpress1
        ServerName example1.wp.localhost
    <VirtualHost *:80>
        DocumentRoot D:\xampp\htdocs\wordpress2
        ServerName example2.wp.localhost