I’ll go step-by-step on how to create a virtual host in the XAMPP environment For Windows.
As we know, the default http://localhost points to C:\xampp\htdocs
as the root directory. The desired result is to be able to visit http://examplevhost.local, with the root directory being C:\xampp\htdocs\examplevhost
.
Note: The steps below are done on Windows 10, but they should also work on other Windows versions (7, 8, 8.1).
Note: I’ll assume that XAMPP is installed in
C:\xampp
. If it’s different on your setup, please read carefully and adjust accordingly.
Note: This should be done only once per XAMPP installation. If you want to add another virtual host later you can skip to the next step.
- Open this file with
notepad
C:\xampp\apache\conf\httpd.conf
- Find the line that looks like this:
# Virtual hosts
# Include conf/extra/httpd-vhosts.conf
- Uncomment the one that starts with "Include":
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
- Make a new folder in the
htdocs
with the nameexamplevhost
- Add a new HTML file to this folder
index.html
- Open this file with
notepad
as administrator
C:\Windows\System32\drivers\etc\hosts
- Add this line to the bottom of the file:
127.0.0.1 examplevhost.local
Note: You should add this line
127.0.0.1 localhost
once in thehosts
file (if it is not already exists)
- Open this file with
notepad
C:\xampp\apache\conf\extra\httpd-vhosts.conf
- Since we enabled this file in the first step we can start editing it. Add this:
<VirtualHost *:80>
<Directory "C:\xampp\htdocs\examplevhost">
AllowOverride All
</Directory>
ServerAdmin your_mail@provider.com
ServerName examplevhost.local
ServerAlias www.examplevhost.local
DocumentRoot "C:\xampp\htdocs\examplevhost"
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Note: You can re-use this snipped for any number of virtual hosts. The directives that need changing are
DocumentRoot
,ServerAlias
, andServerName
(ServerName
must exist as an entry inC:\Windows\System32\drivers\etc\hosts
).
Note: It's safe to remove the example virtual hosts that come by default.
- Restart Apachec server from XAMPP control panel
- Visit http://examplevhost.local and you should see the content of
C:\xampp\htdocs\examplevhost\index.html
.