51

How can I set up virtualhost for multiple domain name on Windows?

I will use it for my own test projects. I have three projects that I need to set up and at the moment I'm using xampplite for the portable Apache.

  1. www.foo-bar.com --> direct to c:\xampplite\htdocs\foo-bar\
  2. www.abcdef.com --> directo to c:\xampplite\htdocs\abcdef\
  3. www.qwerty.com --> direct to c:\xampplite\htdocs\qwerty\web\

I also need to access on another project, but it just like typing http://localhost/my-project/

How can I write the vhost configuration for that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nightingale2k1
  • 10,095
  • 15
  • 70
  • 96

3 Answers3

122

You need to do several steps in order to make this work.

  1. Update the hosts file. On Windows XP, you can find it under c:\WINDOWS\system32\drivers\etc\. You should already see the first line from below. It takes care of your mentioned other project. Add the additional ones to make any requests to the mentioned virtual hosts routed back to your own machine.

     127.0.0.1       localhost
     127.0.0.1       foo-bar.com
     127.0.0.1       abcdef.com
     127.0.0.1       qwerty.com
    
  2. Update the vhosts file in Apache configuration. Under your XAMPP folder, add the following to apache\conf\extra\httpd-vhosts.conf and if needed change the ports (i.e., if you use 8080 instead of port 80).

     <VirtualHost *:80>
         DocumentRoot C:/xampplite/htdocs/foo-bar/
         ServerName www.foo-bar.com
     </VirtualHost>
     <VirtualHost *:80>
         DocumentRoot C:/xampplite/htdocs/abcdef/
         ServerName www.abcdef.com
     </VirtualHost>
     <VirtualHost *:80>
         DocumentRoot C:/xampplite/htdocs/qwerty/web/
         ServerName www.qwerty.com
     </VirtualHost>
    
  3. Do a quick configuration check. Open {XAMPP-folder}\apache\conf\httpd.conf your file and make sure that the following part is not commented out by a preceding # character:

     Include conf/extra/httpd-vhosts.conf
    
  4. Restart XAMPP.

... and you should be all setup now. Your other project should be accessible at the URI you mentioned if you just put it under C:/xampplite/htdocs/my-project/.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MicE
  • 5,038
  • 2
  • 29
  • 26
  • I've been working with a different port and haven't had any luck getting this to work properly on windows. Do you have any advice on this? Using Port 8080 for example. – Jessy Mar 20 '14 at 14:05
  • 2
    One thing which you probably figured out already is that you need to change the vhosts file, i.e. use ``. But everytime I do this, I forget to tell Apache to listen on that port. Look for "`Listen`" in your `httpd.conf` file, and set it to `Listen 8080`. Plus if you're still on Apache 2.2, you may also need to add `NameVirtualHost *:8080` on top of your vhost settings in `httpd-vhosts.conf`. – MicE Jun 21 '14 at 07:08
  • 2
    Thanks. You saved me by mentioning that elusive check for Include .../httpd-vhosts.conf! I thought it'd be included by default but it wasn't and the tutorial I was using didn't mention it. Works like a charm now. – AturSams Aug 10 '14 at 13:08
6

To get C:/xampp/htdocs/my-project/ working, I had to add the following (default?) VirtualHost to apache\conf\extra\httpd-vhosts.conf (in step 2 of MicE's tutorial).

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
</VirtualHost>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JeroenVdb
  • 798
  • 1
  • 12
  • 21
  • maybe you have also to make sure that yo uallow the directory to be read ``` DocumentRoot "C:/my-project" AllowOverride All Require all granted ``` – ßastian Apr 10 '18 at 08:33
1
127.0.0.5  abcd.com

<  VirtualHost 127.0.0.5 >

    ServerName abcd.com

    DocumentRoot "C:\xampp\htdocs\laravel\public" 

    <Directory "C:\xampp\htdocs\laravel\public">

        DirectoryIndex index.php

        AllowOverride All

        Order allow, deny

        Allow from all

    </Directory>

< / VirtualHost > 
Swati
  • 28,069
  • 4
  • 21
  • 41