Virtual host is one of the best ways to host multiple domains. Virtual host can be IP based or name based. IP based means that difference IP address of different websites and home based means running multiple names in each IP address.
In local PHP development environment like xampp/wampp, virtual host is also helpful because it reduces base url problems and it is better to hit
In this tutorial I am going to show you how to setup multiple hosts on xampp. Because as a PHP developer, We are usually running multiple projects at a same time.
If you don’t have xampp on your local machine or you want to install xampp on your machine. Please read my below 2 posts according to your OS.
Steps: Setup multiple virtual hosts on XAMPP
- Create folder in
xampp\htdocsfolder. I will create
- Add virtual hosts for test1 and test2
C:\Windows\System32\drivers\etcand add host for test1 and test2 and save hosts file as a administrator.
- Restart Xampp
I have installed xampp on my E:\ directory on windows.
Create Folders in Htdocs:
Go to your
htdocs folder and create 2 folders
test1 and test2. Now create
index.php file in both of the folder and write “This is virtual host 1” and “This is virtual host 2” in
Now go to
xampp/apache/conf/extra and open
httpd-vhosts.conf. It wil look something like below.
Go to the last line of the file and paste below code.
- VirtualHost is used as a group of directives what will apply on a particular virtual host. In my case, I have 2 virtual host test1.local and test2.local.
- DocumentRoot directive is used to declare directory route what will bind to server name.
- ServerName directive set request url. ServerName is used to uniquely identify a virtual host.
- AllowOverride All is used to set allow the use to .htaccess with in the current virtual host
- Require is used for access control and current projects it is local.
Open hosts and add hosts:
C:\Windows\System32\drivers\etc and open hosts file. Hosts file will look something like below.
Now go the last line of the file and paste below code.
Save hosts file as an Administrator user. If you don’t have administrator rights then copy hosts file to the desktop add above lines and then again copy desktop hosts file to the original and replace to the previous file.
Restart apache and mysql services on xampp and to go browser and type
test1.local in tab and press enter and in other tab type
test2.local and press enter. You will see below page.
If you will face any problem implementing above. Please feel free to post your question in below comments.
- How to Add Custom Sidebar in WordPress
- How to install Composer on Windows with XAMPP