Why Virtual Hosts?
Virtual Hosts allows us to define our own domain name during the development process. How cool is it to have something like www.myapp.dev instead of localhost/myapp? It is always better to setup up virtual host when you are working on multiple applications. It makes the url much more cleaner during the development and also it will give us different cookie and sessions if we are working on multiple apps at the same time. Setting up a virtual host is very easy with just 2-3 steps involved.
How to Setup Virtual Host on XAMPP in Windows
With setting up virtual host you are no longer required to put your source code inside the xampp/htdocs folder. You can put your source code anywhere you want. For this example I am going to setup my app in D:\myapp.
- Open C:\xampp\apache\conf\extra\httpd-vhosts.conf
- Add the following block of code to make the default localhost working even after adding our virtual host
1234567<VirtualHost *:80>DocumentRoot "C:\xampp\htdocs"<Directory "C:\xampp\htdocs">Order allow,denyAllow from all</Directory></VirtualHost>
- Add the following block of code after that.
Shell123456789<VirtualHost *:80>ServerName myapp.devServerAlias www.myapp.devDocumentRoot "D:\myapp"<Directory "D:\myapp">Order allow,denyAllow from all</Directory></VirtualHost>
- Open C:\Windows\System32\drivers\etc\hosts in notepad as administrator.
- Add the following lines to the file.
12127.0.0.1 www.myapp.dev127.0.0.1 myapp.dev
- Thats All. Now you can open your browser and access your app by entering myapp.dev or www.myapp.dev