When we are working in multiple projects and in several folder then virtual host will greatly help in making our job easier and clean. Virtual hosts allow us to set up websites on our localhost accessible using full domain names instead of via localhost/site. It will keep our sites organized and also our URL will be shorter and neat.
For example, if our Xampp is installed in drive C:, then one of our site path may look like C:\Xampp\htdocs\projectsfolder\subfolder\index.php. If we need to access it then, our URL will look like http://localhost/projects/folder/subfolder/index.php. Would it be nice if we can shorten this URL to access the same project ?
How to do it then?
Go to the folder C:\xampp\apache\conf\extra and open file httpd-vhosts.conf in your favorite text editor.
Uncomment the following line to enable name based virtual host on your server’s port 80.
Then add the following code and save it.
<VirtualHost *:80> ServerAdmin email@example.com DocumentRoot C:\xampp\htdocs\projects\myproject ServerName localhost </VirtualHost>
Now go the folder C:\Windows\System32\drivers\etc and open hosts file. Remember, this file should be opened Administrator privilege otherwise we will not be allowed to edit and save it. At the bottom of the page add the following code and save it.
Finally we need to restart the Apache server then we can nagivate our project with URL http://myproject.localhost/
Take another example where we are going to setup two projects. It will be similar for other multiple projects. We have two projects named firstproject and secondproject and their path is C:\xampp\htdocs\firstproject and C:\xampp\htdocs\secondproject. Then in configuration file you will need to add like this.
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot C:\xampp\htdocs\firstproject ServerName first.localhost </VirtualHost> <VirtualHost *:80> ServerAdmin email@example.com DocumentRoot C:\xampp\htdocs\secondproject ServerName second.localhost </VirtualHost>
And in the host file add the following lines
127.0.0.1 first.localhost 127.0.0.1 second.localhost
Now you can access first project with URL http://first.localhost/ and for second project will be http://second.localhost/.
To make work for remaining projects in your folder, you need to set it to default. Following code should be added in the configuration file before the above code.
<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs" ServerName localhost </VirtualHost>
10 thoughts on “How to setup virtual host in Xampp on Windows 7”
How do I setup a second project?
I've tried and they all just send me to the first one
@sean : I have added one example in the post to setup multiple projects. Hope you will understand 🙂
How would I
1. Take a drupal site I have on the web.
2. Copy the site to my local machine.
3. Make changes and then re-install the site on the web?
What files need to be changed to make drupal work with mysql on the local machine and web?
Alias /sf H:/php_project/blogger_system/web/sf
Allow from All
Allow from All
plz tell me how to setup another project in this code.because only one project is running from local host.i want to configure many
After I added the code you added for multiple projects my first.localhost redirects to http://first.localhost/xampp/
and second.localhost redirects to http://second.localhost/xampp/
What have I done wrong this time?
@ sean Have you enabled 'NameVirtualHost *:80' in httpd-vhosts.conf file in line 19 or around.
yes I have
then I have
Nilambar, thanks I figured it out!
Why have you kept same name 'localhost' in myproject also? give it different try once again. 🙂
Hi Nilambar .. I want to access the xamp projects outside local host (windows server 2008) by a laptop connected with internet. How can i do that.