[Apache] Virtual Host Configuration for Multi-Domain Access to Local PHP Projects

  1. First, modify the hosts file located at C:\WINDOWS\system32\drivers\etc. Open it with Notepad and add:

    1
    2
    
    127.0.0.1 www.a.com  
    127.0.0.1 www.b.com
    
  2. Open the file xampp\apache\conf\httpd.conf, search for “Include conf/extra/httpd-vhosts.conf”, and make sure there is no # comment symbol in front of it – this ensures the vhosts virtual host configuration file is included.

    Once httpd-vhosts.conf is enabled, the default httpd.conf configuration becomes inactive (make sure virtual host configuration is also enabled in httpd-vhosts.conf, see step 3). All domain requests to this IP will be directed to the first virtual host in vhosts.conf.

  3. In the virtual host configuration file xampp\apache\conf\extra\httpd-vhosts.conf:
    Remove the ## before NameVirtualHost *:80 to enable vhosts.conf. The default httpd.conf configuration becomes inactive, and virtual host settings will only be configured in httpd-vhosts.conf.

    1
    2
    
    DocumentRoot /xampp/htdocs/a  
    ServerName www.a.com
    
    1
    2
    
    DocumentRoot /xampp/htdocs/b  
    ServerName www.b.com
    
  4. After completing step 3, you will notice that accessing localhost redirects to the path configured for site a. This is explained in step 2 – once vhosts is enabled, the default httpd configuration becomes inactive, and the default access points to the first entry in vhosts. To fix this, you need to add the localhost directory configuration back.

    1
    2
    
    DocumentRoot /xampp/htdocs/  
    ServerName localhost
    

With this, the XAMPP virtual host setup is complete. Now accessing localhost still shows the original XAMPP welcome page, accessing www.a.com points to the bound directory a, and accessing www.b.com points to the bound directory b.