How To Setup Virtual Hosts Using XAMPP
I am currently using Windows 7 and I have XAMPP installed in the default folder so things might be different on your machine just Google them for your OS.
Now lets get underway.
By default XAMPP sets up all the sites you create to have http://localhost
as its top-level domain, and giving your site http://localhost/site
as the url unless you install your site in the top-level folder.
But what happens when you have multiple sites or you want to test out environments which would require you to have different domains, well I am going to teach you to do just that.
The Steps
- You need to have XAMPP installed
- Open up the Xampp control panel and stop Apache (Ensure that you don't have it running as a service.)
-
Navigate to
C:/xampp/apache/conf/extra
or wherever you installed xampp -
Fire up your text editor with administrative privileges and open up
httpd-vhosts.conf
found in theC:/xampp/apache/conf/extra
folder - At the very bottom of the file paste the following
NameVirtualHost *:80
<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs" ServerName localhost
With out that line of code you will lose access to your default htdocs
directory. IE. http://localhost/
will be inaccessible.
- Now copy and paste the code below.
<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/testsite" ServerName testsite.dev ServerAlias www.testsite.dev <Directory "c:/xampp/htdocs/testsite"> Order allow,deny Allow from all </Directory> </VirtualHost>
For Persons using the latest version of Apache (at time of this update 2.4 +) use the code below as the above code is for Apache versions below 2.4
<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/testsite" ServerName testsite.dev ServerAlias www.testsite.dev <Directory "c:/xampp/htdocs/testsite"> AllowOverride All Require all Granted </Directory> </VirtualHost>
Note : Change everywhere you see test site to the location of your site and the domain name you would like to use
The most common ones are .dev, .loc and .local (I believe anything except the traditional .com / .net domains)
-
Now we head over to our Windows Hosts File, to edit the HOSTS. the file will be located at
C:/Windows/System32/drivers/etc/hosts
, where hosts is the file.
127.0.0.1 localhost
look for the line above, and enter your site mimicking the layout
127.0.0.1 localhost 127.0.0.1 www.somesite.dev 127.0.0.1 www.multisite.dev 127.0.0.1 demo.multisite.dev 127.0.0.1 www.testsite.dev #change this to the domain name you chose earlier
change it to reflect the lines above (if you have problems saving it meant you didn't have your text editor running in admin mode.so open editor with administrator permission.
- Restart Apache and Enjoy.