How to Configure Virtual Hosts in XAMPP: Custom Local Domains (e.g. mysite.local)
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.
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.
📋 Guide Table of Contents
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
Add VirtualHost Block in httpd-vhosts.conf
Edit C:\xampp\apache\conf\extra\httpd-vhosts.conf:
<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>
Map Hostname in Windows Hosts File
Add to C:\Windows\System32\drivers\etc\hosts:
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
Google AdSense In-Article Matched Content Unit