How To Set Up Virtual Hosts In XAMPP

/ Misc / by Paul Robinson / 43 Comments
This post was published back on June 5, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

Virtual hosts allow you to set up websites on your localhost accessable using full domain names instead of via localhost/site. While they are extremely useful they can be incredibility difficult to set up if you aren’t used to working with apache.

Why would I need One?

A virtual host can be helpful for setting up a new local projects. The main reason for using one is that it will help keep your sites organized & your URL’s will be nice and neat.

So How Do I Do It?

This guide is for XAMPP since that is the local server I have, but I’m fairly confident it will work for most Apache based Windows local servers.

First create a new folder for your new site/project inside your XAMPP’s htdocs folder. This will be where all your files go, as normal. Next you need to go to your apache folder. This is generally in the xampp folder along with your htdocs folder. Then into conf, and then extra. The full path will look something like D:\xampp\apache\conf\extra.

Next open the file called httpd-vhosts.conf in a text editor. Notepad or Notepad++ are generally the best. Inside the file there will be a lot of lines pre-ceeded by pounds/hashes (#). After then add this:

You just need to change the email (if you are using that), the root to the path to the folder you created earlier, and the server name & alias to the domain name you want to use. Remember that if you use a real, existing domain name you will no longer be able to visit that site once you are finished. That’s why I use a unique domain name & a tld that (as far as I’m aware) is very uncommon, or doesn’t exist.

One more thing you need to do is enter another rule for your localhost. If you don’t you won’t be able to access your standard localhost any more, at least that’s what happened to me. So add this to your httpd-vhosts.conf file too.
[code]
<VirtualHost *:80>
ServerAdmin webmaster@dummy.example.com
DocumentRoot "D:\xampp\htdocs"
ServerName localhost
ServerAlias localhost
</VirtualHost>
[/code]

Now that you’ve done that there are just two steps left. First go to C:\Windows\system32\drivers\etc and open hosts in a text editor, again Notepad or Notepad++ are the best. At the very bottom add:

Where domain.lh is whatever you entered for the domain name & alias above. For each virtual host you add you will need to add another line to your hosts file. If you don’t Windows will not be able to route you to your local site & it will try to use your ISP’s DNS mapping system which will, of course, no nothing about the site.

The final step is to restart apache. So either use the control panel or run the xampp_restart file in the root of xampp. Once that is all finished you just need to try to access your site in a browser, if it loads great, if not check out this troubleshooting tip.

Troubleshooting

If you haven’t been able to access you local site, you can try this tip to help. Make sure apache has actually restarted. Sometimes it seems to restart, but hasn’t. Flush your DNS cache using:

at a windows command prompt. I can’t confirm that this helps, but I’ve read reports of it working for others. Finally make sure you have no spelling mistakes in your Virtual Host blocks. If you have it will NOT work.

I wrote this tutorial after I received a few requests asking how to set up virtual hosts in XAMPP, so I hope it helps answer that question. As always if you have any further questions, or suggestions please drop them in the comments. Also don’t forget if you like Return True & want to help out you can donate to the server fund or buy me a gift from my Amazon wish list using the links below.

43 Comments

Author’s gravatar

Great tip and useful.
I tried to use this tip, but when browsing to my localsite I’ve got an /xampp trailing added automatically to the url and the browser loaded the regular XAMPP screen.
I use XAMPP 1.7.3 on XP Pro.
Do you know why is that happening?
Thanks

Reply
Author’s gravatar author

Generally that happens when visiting the localhost root. /xampp is added to redirect you to the xampp admin. Normally adding a folder name you get taken to the folder.

Also generally adding in the section to your vhosts file for just the localhost stops that from happening.

Also as mentioned in the article if you are having problems restarting xampp is generally the best thing to do.

I do know that you are supposed to redirected to the xampp interface when visiting the localhost root, but if you’ve added the localhost vhost & it’s still doing it, I’m afraid I’m not sure why it’s happening.

Author’s gravatar

Well yes, it really adds the /xampp when I browse to http://localhosts.
But I also added the localhost vhost to the .conf file, as you suggetsted, and for sure I restarted my XAMPP and even my browser .. Weird issue

Author’s gravatar

I changed now the documntRoot of the localhost to this –
DocumentRoot “D:\xampp\htdocs\xampp”

and now browsing to http://localhost does not adding the /xampp to the url and the server screen is viewable.
But when browsing my vhost, I still get the XAMPP page instead of the local site that in the targeted folder.

Author’s gravatar author

DocumentRoot should be set to the file path to the folder which holds the data for your localsite. The xampp folder is the folder that contains the xampp configuration page.

The vhosts system is generally used so you can set up a local clone of a site & keep the URL structure intact. So for example you could have a website called mywebsite.com & have a local version called mywebsite.lh. Then it is easy to keep track of changes etc as both sites have the same URL structure except for the TLD.

If you keep getting redirect to /xampp or the page at /xampp no matter what you set the document root to I’m not sure what is happening. Although you could check your /htdocs folder for a .htaccess file with some sort of redirect rule in it.

Author’s gravatar

Yes, I know that and I use vhosts on lynux in the office, but @home I use XAMPP on XP.
Anyway, the change I wrote that I made was to the DocumentRoot of the localhost itself, not to the vhost I try to set.
This is my httpd-vhosts.conf:

ServerAdmin webmaster@dummy.example.com
DocumentRoot “D:\xampp\htdocs\xampp”
ServerName localhost
ServerAlias localhost

ServerAdmin webmaster@dummy.example.com
DocumentRoot “D:\xampp\htdocs\tests”
ServerName maor-tests.com
ServerAlias maor-tests.com

Author’s gravatar author

Assuming WordPress (the pain that it is) stripped out the VirtualHost tags that looks okay. My only advice is not to use real TLD’s for local tests & to make one of you own up such as lh.

Also have you entered the domain name into your windows hosts file? If not your newly added test won’t work.

Your localhost one should just point to the htdocs folder as it did before. The only thing I can think of is an htaccess file in the root of the htdocs folder is redirecting you to the xampp folder. I don’t have that htaccess file there anymore so I can’t say for sure if there is one or not.

Author’s gravatar

Yup, it appears that WP stripped out the VirtualHost tags…
Ofcourse I edited my hosts file
I tried to replace the TLD to .lh and restart the XAMPP again and the broswer andedited again the hosts, now it is –
127.0.0.1 maor-tests.lh

and the VH in the conf is –

ServerAdmin webmaster@dummy.example.com
DocumentRoot “D:\xampp\htdocs\tests”
ServerName maor-tests.lh
ServerAlias maor-tests.lh

but it still the same, browsing to the XAMPP opening page.. 🙁

Do you have any more ideas why could it be like this?

Thanks a lot for your quick help !

Author’s gravatar author

The only thing left is that htaccess file.

If there is not htaccess file though I’m not sure as it would suggest it is ignoring your vhosts file.

Author’s gravatar

I had a .htaccess file in the /tests folder, which I deleted , but it still the same.

Reply
Author’s gravatar author

If when you visit moar-tests.lh you are being redirected to the xampp page, it’s a little bit of a mystery I’m afraid.

Last resort (and stab in the dark) is that maybe the include for the vhosts file is commented out in the httpd.conf file meaning it would ignore it. However as altering the DocumentRoot changed things before it probably isn’t the case.

I’m afraid I’m out of ideas. The instructions above have always worked for me & a few of my friends. Sorry. 🙁

Author’s gravatar

I checked the httpd.conf, and that line isn’t commented out there ..

It is very weired issue. Well, it’s not that matters, since I am used to work with http://localhosts/sitename for a long time, but it looked nice to me and nevert thought about the option to have Vhosts in XAMPP.
And now since it’s not working it’s just driving me nuts to try to solve this 🙂 not that I really need it…
I looked at your site in many other posts and it’s really great stuff you have here! So I came across this post during the surfing here.
If I’ll find something about the XAMPP I’ll post here a comment about it..

Author’s gravatar author

Sorry I haven’t been able to solve the problem. I’ll post a comment if I find anything about it. 😉

Thanks. I need to do a new post I haven’t had much time lately. 🙁

Author’s gravatar

One rule I learnt about blogging – never to apologize about not posting for several time. This is the mean of a blog – one posts when one has something to post about, or the time to write it.
Your RSS subscribers will know about the post when you write it.

Author’s gravatar author

Haha. Very true, I never thought about it like that before. Thanks. 🙂

Author’s gravatar

@Maor : Tried point 7 here : http://www.satya-weblog.com/2010/01/wordpress-php-install.html ?

Reply
Author’s gravatar

@Ashish Thanks, I think I already tried it then to add these to constants to wp-config, but it didn’t help.
Anywway, that isn’t too much important, since instead of going to dev.mysyte.com, I’ll just browse to localhost/mysite
that’s not a big difference.
It’s a nice thing to have and just wanted to give it a try, but not a must..
Thanks anyway

Author’s gravatar

Maor, I don’t know if you are still monitoring this thread or if this will even work, but I was having the same issue as you (and banging my head against the wall as a result, let me assure you).

I uncommented this line near the top of my .conf file:

NameVirtualHost *:80

I noticed that it was uncommented in the Apache examples and gave it a try. That seems to have done the trick.

Reply
Author’s gravatar

Mark, thank you, yes, it is really this line that should be uncommented, I realized that a while ago and it did solve the issue.

The only problem is that after doing this change, I had to change and make a virtual host for each one of all the folders inside the xampp/htdocs (each folder contains a website/webapp), since after the change they were not accessible anymore via the url http://localhost/foldername

More than that, in WordPress I had to change the WP SiteUrl and SiteHome options and to resave the permalink structure – without doing that , only the homepage of the site was available but not the inner pages.

Author’s gravatar author

That happened to me a while ago. I never fixed it but when I reformatted (I keep XAMPP on a different HDD) it suddenly started working again & I could access my virtual hosts via their hostname & via the localhost/foldername. No idea what happened though. Still bugs the crap out of me to this day.

Author’s gravatar

Well Paul it is interesting that after a format it was ok again. Meaning that it might be something in the XP registry and not only in the XAMPP and virtual hosts settings.. If you come up with an idea what cause this I will be happy to hear (right now, I haven’t created yet all the virtual hosts for all the folders, since it is too much..)

Author’s gravatar

Mark,
Thank you, I was having the same issue now on 2012 and I wasn’t able to fix it till now.

Author’s gravatar

Hi,

I tried this in ubuntu, following your steps, but when I try to access the virtual host, it opens /opt/lampp/htdocs which is the same root folder of the “localhost” virtual host. What am I missing?

thanks

Reply
Author’s gravatar author

Hi Matteo,

I’m afraid I haven’t used Linux in a while, but I don’t think the process is that much different.

Have you placed the part that you would normally place in the Windows hosts file in the equivalent file within Ubuntu? Also have you added the bit of code Mark suggested if it isn’t already there?

Author’s gravatar

Thanks for your answer. Yes to both: edited /etc/hosts (if I don’t, browser won’t find any virtual host at all and look for a “real” domain which doesn’t exist) and yes, the line mentioned by mark was already uncommented.

Anyway I solved it in another way, not at all the same but ok for me; instead of creating a virtual host, I placed a symbolic link _inside_ htdocs to the other folder, so when I go to the localhost, I see the symbolic link and follow it.

This didn’t work at the beginning because of permissions issues and I got a forbidden error; now I’ve fixed the permissions and this works. After fixing permissions, I haven’t tried the virtual host solution though.

However with the virtual host I didn’t get any forbidden error, it would just load the localhost virtual host instead of the other virtual host. So I don’t think the permission issue was the cause of the fault with the virtual host.

Anyway I’m happy with the symbolic link setup and don’t really need the virtual host, though I do wonder how people set up their virtual hosts in linux

Author’s gravatar author

I’m sorry I’m afraid I’ve no idea why it isn’t working the correct way.

Nice idea with the symbolic linking to get around it though. 😉

Author’s gravatar

@Matteo, I haven’t exactly understood your symbolic link solution.. Could you please explain it bit more in some words?
Thanks

Author’s gravatar

@Maor, mine is not a solution at all if you really deen virtual hosts. It just happens to be ok for me, because all I need is to be able to access an arbitrary folder in my filesystem.

I don’t create any virtual host at all. In my htdocs directory (where by the way I deleted the index.php files so I can see the contents of the directory) I created a symbolic link to a folder that is somewhere else in the filesystem.

So when I open “localhost” in the browser, I see the contents of the htdocs directory. I can click on the “xampp” subdirectory to open the administration page, or on the symbolic link to access the folder that contains subfolders with all my projects.

Hope this is clearer 😉

Author’s gravatar

Maor, i do not know if you have fixed your problem yet, but try changing the line to . also in your computers host file, replace every mention of localhost with mywebsite.lh

Reply
Author’s gravatar

Hello guys! I had the same issue six months later. I knew I had it configured correctly before however, when I tested my VH today, suddenly it no longer works. I’m using XAMPP 1.7.3 as well. Weird stuff 😐

Reply
Author’s gravatar author

Sorry to hear it’s no longer working Prezire.

I’m sorry I can’t help more with Linux configurations. Most of the work I do is on my local Windows computer & I only ever use *nix when I SSH into computers for clients.

I wish I could help more.

Author’s gravatar

I had the same problem. It seemed like the domain name set in the hosts file won’t resolve to the virtualhost in xampp.

It was a problem with the browser. I’m using google chrome. Try to close all instances of the browser and try again. It should work.

Reply
Author’s gravatar author

Thanks for the tip. I’ve never though about it being a browser problem before.

Author’s gravatar

+1 – I have seen the exact same browser issue so I’ve learned not just to restart Apache but also to close at least the tab and open a new one when I’ve made such VirtualHost changes. I didn’t have to close all Chrome processes though. I also do a lot of browser juggling between Chrome and Firefox.

Author’s gravatar

For the benefit of people googling to find a solution when they face the same issue (which was my case yesterday), if you’re using XAMPP on Windows you should use forward slashes instead of backslashes in your directories, as in:

DocumentRoot “D:/xampp/htdocs/tests”

Reply
Author’s gravatar author

Hi Olivier,

Very strange. If I use forward slashes it stops my XAMPP from working & I’m running Windows 7. I’ve always had to use backslashes in paths on Windows. Even when writing BAT files or PHP on Windows I’ve always had to use backslashes.

I wonder what causes the difference?

Author’s gravatar

Paul,

I don’t know, I just shared what ended up working for me after a few hours of fumbling around (which is how I found this thread). I also ended up moving my local WP install out of /htdocs, as well as setting up localhost on a separate TCP port to serve the XAMPP admin interface, because I kept having subdirectory conflicts. Now I have 2 different virtual hosts on 2 different ports served from 2 entirely separate (file system) directories and that solved my various problems. For any page on my WP site I can switch from local.mydomain.com to the same page on http://www.mydomain.com with just one click thanks to a pretty nice Firefox extension called ServerSwitcher. And I load the XAMPP admin pages via localhost:81.

I still have to figure out local SSL though!

Reply
Author’s gravatar author

It’s all very strange. 🙁

I don’t claim to be an expert on XAMPP or Unix systems, most of it is way above me. I think I’ll just stick with PHP & the little bits I do know, lol.

Nice setup though. 🙂

Author’s gravatar

Hi Paul,

This worked fine but i had a wierd problem which i dont see anyone else has

I made the changes in my httpd-vhosts.conf as

ServerAdmin sg@saurabhgoswami.com
DocumentRoot “D:\localhost\htdocs\saurabhgoswami”
ServerName sg.gos
ServerAlias sg.gos

ServerAdmin sg@saurabhgoswami.com
DocumentRoot “D:\localhost\htdocs”
ServerName localhost
ServerAlias localhost

and in the C:\Windows\system32\drivers\etc\hosts
as

127.0.0.1 localhost
127.0.0.1 sg.gos

when i am trying to access sg.gos it redirects to to right folder however, when i try to access localhost it redirects again to the sg.gos folder. So i am unable to access localhost 🙁

Reply
Author’s gravatar author

Hi,

I did have that problem once, but it turned out to be a spelling mistake in my localhost rules. I can’t see any problem with yours so I’m not sure what it is. One other thing I can think of. Do you have:

in your vhost file? I seem to remember that if it’s missing that can cause a similar problem too.

Author’s gravatar

I spent more than 4hours trying to fix mine and then stumbled upon your blog.. Your comments did the trick for me, i must say.. Well, I changed my forward slashed to backward slashes and woooola, it’s working… nice blog, i will definitely bookmard this…

Reply
Older Comments
Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more