To setup apache virtual hosts using XAMPP in windows when working in local environment, I like to setup my projects with separate domain names like projectname.stage to keep them isolated from each other.
It’s fairly easy to get this working using XAMPP in Windows. In order to do that, there are 2 main steps which are:
Open the following file in any text editor.C:\
In my case, It isF:\xampp\apache\conf\extra\httpd-vhost.conf
The file will look like this:# Virtual Hosts # # Required modules: mod_log_config # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn’t need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at #
Paste the following at the bottom of the httpd-vhost.conf file.
The ServerName entry specifies which domain to associate with your project (in this example: projectname.stage). The DocumentRoot entry is the folder that will be loaded by Apache when visiting the domain specified by ServerName.
Note: Apache will first try to load up index.php file in that directory, if there’s no index.php, it’ll look for index.htm|index.html, if it can’t find any index file, it’ll show the file listing of that directory.
Open the hosts file to add an entry that tells the computer to point the above domain back to our own machine.
Open the following file in a text editor with Administrator rights, (right click -> Run as administrator):C:\Windows\System32\drivers\etc\hosts
It will look like this:# Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a ‘#’ symbol. # # For example: # # 126.96.36.199 rhino.acme.com # source server # 188.8.131.52 x.acme.com # x client host # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost #127.0.0.1 localhost
Simply add the following line at the bottom of this file.127.0.0.1 projectname.stage
This will route all requests to projectname.stage to the IP Address 127.0.0.1 which is the loopback address of the machine, which just points to itself.
Restart Apache, So it can load the new configuration.
If you have any questions, feel free to leave a comment and I will get back to you.