Thursday, January 4, 2007

Making subdomains on localhost

Intro
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 situated on different folders.

PS: This is for Apache-Windows configuration. (If you have info about other Server-OS configs please let me know.)

Steps

First decide on the subdomain names...
for eg:
sub.localhost, sub2.localhost, alt.localhost , images.localhost etc..
You can also name 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.)

Second making these sites to point to 127.0.0.1, for this you can edit the HOSTS file on windows. See this about the HOSTS file.

Now add to HOSTS line
127.0.0.1 hostname
Egs:


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


Next we have to assign each of these different URL's to different folders.
This is done on the apache conf file. (httpd.conf file situated in apache\conf\ directory)

Scroll all the way down the httpd.conf file until you reach about Virtual Hosts.
Add

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>

Egs:

<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>


Test the configurations (using apache -t)
All done!
Hope it worked for you ...
Feel free to leave a comment....

24 comments:

Chad said...

I actually wondered how to do this. thanks for taking the time to write a step by step instruction.

sarath-a said...

MARVELLOUS sure i wud try this one!

Anonymous said...

I tried making a vhost entry for each client I have (client1.localhost, client2.localhost, etc.), but when I go to any of them it always goes to the first entry, client1.localhost. What could be causing this?

Anonymous said...

I found the reason for the error stated in my last comment about it going to the first vhost. It was because I wasn't using name-based virtual hosts (NameVirtualHost *:80).

Anonymous said...

Thanks hips!

Anonymous said...

For Linux, the procedure is exactly the same, except the hosts file is located at /etc/hosts (use sudo or login as root). For each sudomain, you add a line with:

127.0.0.1 subdomain.localhost computer-name

Anonymous said...

thanks . its working.

Unknown said...

Just something else to add:
Be sure to include a default VirtualHost location. It's a good idea to create a VirtualHost entry to point to your plain old http://localhost/'s root directory, to avoid getting any "cannot find on this server", or 403 forbidden errors:

<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/path/to/your/htdocs"
DirectoryIndex index.php index.html index.html index.htm index.shtml
</VirtualHost>

There's a handy reference for Name-based Virtual Hosts at apache.org.

Anonymous said...

This was really helpful. Thanks.

Anonymous said...

I found this quite useful. Unfortunately you still have to add each subdomain to your hosts file, but it saves having to make additions to your httpd.conf file. It takes your subdomain and maps it to the directory with the same name. I believe the directory must be lower case, and you may need apache's rewrite_module enabled.

<VirtualHost *:80>
ServerName localhost
ServerAlias *.localhost
VirtualDocumentRoot "C:/Users/Simon/My Websites/%1"
DirectoryIndex index.php index.html index.html index.htm index.shtml
</VirtualHost>

Anonymous said...

On mac, host file is at /etc/hosts

underdogmedia said...

Best tutorial I have found! Worked perfectly. Thanks a million

Unknown said...

How do i create dynamic sub domain, i dont have to make entry in hosts file every time.
For eg. if new user is registered "Ajey" then he should be able to access ajey.mysite.com

I tried *.mysite.com in hosts file but didnt work

Arun Prabhakar said...

ServerName video.localhost
DocumentRoot "E:\server\htdocs\video"
DirectoryIndex index.php index.html index.html index.htm index.shtml

Are you doing this for a virtual host ?

Unknown said...

HI...

how about windows 2003 server?

could you help me pls?

Unknown said...

It is what to create only subdomain,
but how can we create subdomain.subdomain.localhost
some thing like:


sajid.webspot.localhost,
nasir.webspot.localhost,
naveed.webspot.localhost,
zeeshan.webspot.localhost,
ihsan.webspot.localhost,
etc..

Arun Prabhakar said...

Same steps apply

Unknown said...

Does this work public?

Anonymous said...

I tried this, but then both localhost and the subdomain both pointed to the subdomain. It took me awhile, but I figured it out:

Be sure you are using name-based virtual hosting, as mention in comment #4.

Add a virtual host pointing back to localhost, as mentioned by Chris in #8.

In each virtualhost, add the following to make sure that you will be allowed to access the virtualhost:


Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all


Hope this helps!

Dragos said...

Brilliant tutorial, it helped a lot!
Btw, works just as well on Linux :)

Anonymous said...

Great Info.

Just fyi, I am using UniServer and by editing the hosts file did the trick. I didn't need to edit the apache config (so far at least).

Jon Piehl said...

FYI - You may need to restart your server for this to work properly.

Thanks for the info. It helped me a lot!

Unknown said...

Worked like a charm on Ubuntu 10. The host file is /etc/hosts, pretty much the same on all Linux systems, but the virtual host definition was put on a new file in /etc/apache2/sites-enabled folder.

Ganesh said...

For a wordpress installation, if I want to use these subdomains and point them to the sites that I create under a multi-site network, what folder path must I give?

/htdocs/wordpress/sub?

Also, I didn't even have the 'NameVirtualHost *:80' commented. I didn't even have that piece of text in my conf file. So I added it. Any cause for concern?