How To Set Up Apache Virtual Host on Windows

Introduction

As a webmaster, upon creating your first site, you may want to install a program like XAMPP. You then put your site in the /htdocs/ directory and it is then accessible via the URL: http://localhost/

Issue

But will happen if you start to develop several sites simultaneously ?

You end up with a multitude of sub folders, and URLs to extension, for example:

http://localhost/folder1/folder2/myproject/index.php

Which will act as the site root url in the development phase.

The problem is that absolute and relative URLs do not work when you host your site (live) and sometimes you may be confronted to lengthy urls to access the site, etc. …) then this tutorial will be helpful.

The objective of the tutorial

We will create virtual host and use short addresses such as: http://myproject.dev/

Prerequisites

  • This tutorial was written for people working under Windows environment, and using XAMPP server. But you will probably have no difficulty to implement this method with other software.
  • Assuming you have installed XAMPP under C:xampp.
  • Assuming you have placed your current project under C:xampphtdocsMyProject.
  • You need a text editor, such as Notepad.

Set the host for windows

Open the file: C:WindowsSystem32driversetchosts
At the end of the file you should have something like this:

127.0.0.1 localhost
::1 localhost

Add the following line to the end of it or below the above written lines

127.0.0.1 myprojet.dev

And save the file.

Note: If you can not edit the file, verify that it is not read-only. Under Vista or Windows 7, you may encounter more problems with the UAC. Make a copy of the file on your desktop, set the write permissions, make the change, then put the file in read-only, and overwrite the original file.

Configuring Apache

Find the httpd.conf file in the apache folder inside xampp.

C://xampp/apache/conf/httpd.conf

Open the above file and add the following lines,

It is essential to properly put the two host definitions. 1st one to retain your original localhost definition, and the 2nd one for your desired host name.

Save the file.
Restart your Apache server.