How to create subdomains on localhost

Web hosting, SEO, etc... related
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to create subdomains on localhost

Post by Neo » Sun Jan 03, 2010 12:45 pm

Introduction
Developing websites on localhost may sometimes need making of sub domains. Installing and running another server for this purpose is useless, consumes more resources etc. So here is the technique to make sub domains on localhost.

You can also use this to host more than one site on localhost. i.e, you can host multiple sites on different folders.

This is for Apache-Windows configuration.

Steps
  1. Decide on the subdomain names...
    For ex: sub.localhost, sub2.localhost, alt.localhost , images.localhost etc..
    You can also name http://www.name.com. (Please note that if there is a site with that name on the net, you wont be able to access that site on the internet)
  2. Make the sites to point to 127.0.0.1, need to edit the HOSTS file on windows.

    Add to HOSTS line
    127.0.0.1 hostname

    Ex:

    Code: Select all

    127.0.0.1 sub.localhost
    127.0.0.1 sub1.localhost
    127.0.0.1 sub2.localhost
    127.0.0.1 images.localhost
    127.0.0.1 www.mysiteonmycomp.com
    
  3. Assign each of these different URL's to different folders.
    Need to edit apache conf file. (httpd.conf file is at apache\conf\ directory)

    Find Virtual Hosts in the file and then add as follows.

    Code: Select all

    NameVirtualHost *:80 #(This line was commented before)
    
    <VirtualHost *:80>
     ServerName subdomain name.localhost OR yoursite.com
     DocumentRoot Path to the server root, See examples.
     DirectoryIndex index.php index.html index.html index.htm index.shtml
    </VirtualHost>

    Ex:

    Code: Select all

    <VirtualHost *:80>
     ServerName sub.localhost
     DocumentRoot "C:/public_html/sub"
     DirectoryIndex index.php index.html index.html index.htm index.shtml
    </VirtualHost>
    
    <VirtualHost *:80>
     ServerName images.localhost
     DocumentRoot "C:/public_html/images"
     DirectoryIndex index.php index.html index.html index.htm index.shtml
    </VirtualHost>
    
    <VirtualHost *:80>
     ServerName site.com
     DocumentRoot "C:/public_html/site"
     DirectoryIndex index.php index.html index.html index.htm index.shtml
    </VirtualHost>
    
Now you have test the configuration using using apache -t

That's it.
Post Reply

Return to “Web Related”