create localhost subdomains with apache & linux/windows

If you develop multiple websites, you may need to create localhost subdomains, so that you can access them in your web browser using a similar uri: http://site1.localhost where ‘site1’ is anything you want.

Creating localhost subdomains is a two-step process:

  1. you must declare your subdomain in HOSTS file.
    This step is very important, otherwise if you don’t do it, your browser will search the web (and not your local computer) to find your website and you won’t get anything.
    Under windows, HOSTS file is there:

    Windows 98/ME : C:\WINDOWS

    Under linux, it is: /etc/hosts

    Open the file, and append a new line with the following:

    "    site1.localhost"

    replace ‘site1’ with wathever you want

  2. Next step is to edit apache httpd.conf file and declare your subdomain.
    [UPDATE: if you use xampp on windows, you may rather edit /apache/conf/extra/httpd-vhosts.conf]
    Locate your httpd.conf file and append the following:

    <VirtualHost *>
      ServerName site1.localhost
      DocumentRoot "path/to/website"

    Don’t forget to replace ‘site1.localhost’ with the local subdomain name you want to use and “path/to/website” to the path of your website (ex: /var/www/site1).


    • it’s important to wrap path into double quotes in case a folder name contains a space
    • it’s important NOT to add trailing slash/backslash as it will make apache not work!

That’s it!
Restart Apache, open your browser on http://site1.localhost and your website should be loaded


Post a Comment

Your email is never published nor shared. You're allow to say what you want...