Tuesday, December 22, 2009

How to add subdomains.localhost if using XAMPP?

Hi,

After a long gap (nearly 2 years) of no blogging, finally I got my time to blog again. Mainly because I am back active being freelance programmer and interested to share some stuffs that I found with you guys.

I always used XAMPP to run PHP and recently I badly needed to have subdomains under localhost for one of my client's web to work. So I kick my ass and searched for "how to", many was there but missing the important pinch. Therefore I thought maybe sharing my own way would be helpful for someone else out there!

I found http://www.offshootinc.com/blog/2007/12/21/setting-up-a-subdomain-on-localhost/ but not worked because missing double quotes and NameVirtualHost. I continue googling and found this: http://www.ardamis.com/2005/08/11/xampp-apache-namevirtualhost/

That one worked like charm!

To cut the crap short; just add like this in C:\xampp\apache\conf\extra\httpd-vhosts.conf;

NameVirtualHost *:80
<virtualhost>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</virtualhost>
<virtualhost>
DocumentRoot "C:/xampp/htdocs/sub"
ServerName sub.localhost
</virtualhost>

And add this in the C:\Windows\System32\drivers\etc\hosts
127.0.0.1 localhost
127.0.0.1 sub.localhost

I am using "XAMPP for Windows Version 1.6.6a" and it worked fine!

Have fun! But if the above method did not work for your version of XAMPP then continue googling and good luck! Don't ever get disappointed when you can't figure it out. Because I understand that messing with server is the most irritating task (after messing with scripts). And there is definitely a way to do it out there. Keep googling and keep on googling!

Take care ya!

=========== Updates on 26th March 2010 ===========
If you are running Windows 7, then you cannot simply edit the host file. Instead, you must right-click on Notepad and select "Run as Administrator". This would open the notepad.exe application with Administrator permission. Then, open the"C:\Windows\System32\drivers\etc\hosts" inside the Notepad through File > Open. Remember you must view the file-type with "All files" instead of "Text File". After opening the host file, you can edit and save it.

In xampp apache config ("C:\xampp\apache\conf\extra\httpd-vhosts.conf"), you must write like this
NameVirtualHost 127.0.0.1:80
<virtualhost 127.0.0.1>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</virtualhost>
<virtualhost 127.0.0.1>
DocumentRoot "C:/xampp/htdocs/sub"
ServerName sub.localhost
</virtualhost>

That's it! Your Windows 7 can run Xampp with sub domains too.


=========== Updates on 31st October 2014 ===========
The setting above only allows to run single subdomain. To have multiple subdomains, in the httpd-vhosts.conf, set it to be like this:

NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/folderX"
ServerName domainX.localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/folderY"
ServerName domainY.localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
ServerAlias www.localhost
</VirtualHost>

Also remember to update your C:\Windows\System32\drivers\etc\hosts to include the new subdomain like this:
127.0.0.1 localhost
127.0.0.1 domainX.localhost
127.0.0.1 domainY.localhost