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
- 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).
- 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“
- At the bottom of hosts file, add:
- 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).
- Save and close the hosts file.
2nd Step: We need to add our custom host configurations in httpd-vhosts.conf file
- Locate your XAMPP folder, and then follow – apache\conf\extra\ folder. In my case, it looks like something: E:\xampp\apache\conf\extra
- Open httpd-vhosts.conf file in your text editor.
- Navigate at the bottom. Add code like below:
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).
- 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.
- 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).