How to Configure Virtual Hosts in XAMPP: Custom Local Domains (e.g. mysite.local)

Advertisement

Google AdSense In-Article Display Placement

Publisher ID: ca-pub-XXXXXXXXXXXXXXXX (Configured in config.php)

Virtual hosts allow you to access local projects using realistic custom domain names (e.g. http://myproject.test) instead of cluttered subdirectories like http://localhost/myproject.

✔ Rapid Direct Solution

1) Edit "C:\xampp\apache\conf\extra\httpd-vhosts.conf" and add a VirtualHost block defining DocumentRoot and ServerName. 2) Open Notepad as Administrator, edit "C:\Windows\System32\drivers\etc\hosts", and add "127.0.0.1 myproject.test". 3) Restart Apache in XAMPP.

1. Problem Diagnosis & Symptoms

Working in subdirectories under localhost often causes relative URL and cookie path issues when moving to production.

2. Primary Root Causes

  • Cause: Missing virtual host mapping in Apache.

3. Step-by-Step Diagnostic Fixes

1

Add VirtualHost Block in httpd-vhosts.conf

Edit C:\xampp\apache\conf\extra\httpd-vhosts.conf:

APACHECONF
<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/myproject"
    ServerName myproject.test
    <Directory "C:/xampp/htdocs/myproject">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
2

Map Hostname in Windows Hosts File

Add to C:\Windows\System32\drivers\etc\hosts:

TEXT
127.0.0.1    myproject.test

4. How to Verify the Resolution

  • ✔ Open http://myproject.test in your browser.

5. Diagnostic Troubleshooting Matrix

Symptom Observed Root Cause Recommended Action
403 Access Forbidden on custom domain Missing Require all granted in Directory block Add Require all granted inside the VirtualHost Directory block

6. Critical Mistakes to Avoid

  • ❌ Never use .dev as a local test domain (it requires HTTPS by HSTS). Use .test or .local.

7. Frequently Asked Questions

❓ Do I need internet access for local virtual hosts?

No, loopback requests resolve locally on your machine via the hosts file.

Official Technical References

Advertisement

Google AdSense In-Article Matched Content Unit