Setup Virtual Host with XAMPP in Windows OS

A virtual host can point to any folder on any drive of the system, it’s generally used to easily manage if we store all of our sites in a common location (like HTDOCS folder in case of XAMPP).

Virtual host also enables us to use “Preety URL” in our local development environment. Like:

We can Use: http://laravel.dev
Instead of: http://localhost/laravel5-test/public/

In short, virtual host enables us to point any directory or sub-directory by using our given (i.e. custom) URL format.

 

1st Step: We need to edit hosts file first

  1. Make sure to STOP both Apache and MySQL services. You can turn off services from your XAMPP control panel, or you can use Task Manager to stop services (to access task-manager just type taskmgr in run box).
  2. Navigate to this folder – “C:\Windows\System32\drivers\etc“. You’ll find hosts file there. We need to edit this file to add our custom URL as a localhost. Now launch your text editor (like Notepad++) as administrator (i.e. Run as Administrator), otherwise Windows will not allow you to save edits. Open hosts file in editor from “File –> Open” and then inserting “C:\windows\system32\drivers\etc\hosts
  3. At the bottom of hosts file, add:
     

    127.0.0.1       laravel5.dev

     

  4. Here, laravel5.dev is our custom URL, you can use URL as you like. Make sure, there is no “#” symbol before your added/pasted line (because # indicates a comment line and system ignore such lines).
  5. Save and close the hosts file.

 

2nd Step: We need to add our custom host configurations in httpd-vhosts.conf file

  1. Locate your XAMPP folder, and then follow – apache\conf\extra\ folder. In my case, it looks like something: E:\xampp\apache\conf\extra
  2. Open httpd-vhosts.conf file in your text editor.
  3. Navigate at the bottom. Add code like below:
     
    <VirtualHost *:80>
    DocumentRoot “E:\xampp\htdocs\laravel5-test\public”
    ServerName laravel5.dev
    </VirtualHost>
     
    Here, DocumentRoot is the folder path that you want to point, and ServerName is the URL that you want to use (and of course which one you already set in hosts file).
  4. You can set other configurations inside your block. For details options please check top section of httpd-vhosts.conf file. You’ll find commented out examples there.
  5. Save the httpd-vhosts.conf file and close it.

 
That’s it! Now restart your Apache and MySQL server and test your virtual host (by simply typing your custom URL).

Leave a Comment.