Use Custom Domain in XAMPP Instead of LocalHost

0
244
Use Custom Domain Instead of LocalHost in XAMPP
Use Custom Domain Instead of LocalHost in XAMPP

XAMPP allows us to develop and test web server code’s on our local machine. We can configure and test our website locally, instead of live. By default XAMPP sets up all sites that we create to have http://localhost as its top-level domain, and giving your site http://localhost/site as the URL unless you install your site in the top-level folder. But you have multiple sites or you want to test out environments which would require you to have different domains. Well I’m going to show you how to do that. Multiple websites running on the same Apache web server are called Virtual Hosts.

You can setup the Apache Server in XAMPP to serve up web pages as though they were actually located on www.example.com instead of localhost. I’ll explain you how to setup multiple virtual hosts on XAMPP, So you can run multiple WordPress websites or Scripts on the same XAMPP installation.

Configure Virtual Hosts in XAMPP

If you don’t have XAMPP on your machine than you need install XAMPP. If you already have than open Xampp control panel and stop Apache.

To add virtual hosts in XAMPP, or in other words, to configure Custom Domain in XAMPP, You need to open the Virtual Hosts Apache configuration file httpd-vhosts.conf. So Navigate to

C:/xampp/apache/conf/extra
Custom Domain name in xampp
Custom Domain Name Instead of LocalHost in XAMPP Editing in httpd-vhosts.conf file

Open up httpd-vhosts.conf in Notepade++ or Notpad. Navigate to bottom of the file paste the following.

<VirtualHost *>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
</VirtualHost>

With this line of code you will also access your default address. It means you don’t loose  http://localhost. It will be also accessible. Now copy and paste the code below.

<VirtualHost *>
    DocumentRoot "C:\xampp\htdocs\mysite"
    ServerName mysite.com
    <Directory "C:\xampp\htdocs\mysite">
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *>
    DocumentRoot "C:\xampp\htdocs\mysite2"
    ServerName mysite2.com
    <Directory "C:\xampp\htdocs\mysite2">
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>

I am using mysite.com as example you can change it with your custom domain name. But remember the directory name would be your custom domain name.  Like mysite, mysite2 that i set here. You can use multiple custom domain name here like this.

Custom Domain in XAMPP
Use Custom Domain Instead of LocalHost in XAMPP | Editing in httpd-vhosts.conf

The website files can be found in my case

c:\xampp\htdocs\mysite 

c:\xampp\htdocs\mysite2

and will respond to

mysite.com

mysite2.com

For each other virtual host (website) you need to configure on the XAMPP Apache web server.  Now the second part. In second part we need to Configure the Windows Hosts File. It will redirecting the Websites that we set to Your Computer.

Configure the Windows Hosts File

Using  the Windows Hosts file we redirect the traffic on your computer rather than to the original website. Do not use any domain that actually exist other wise you will redirect to your computer rather than to the original website.

Now we need to edit the Host file. The file will be located at

C:/Windows/System32/drivers/etc/hosts
Virtual Host Instead of LocalHost
Use Custom Domain Instead of LocalHost in XAMPP | Locating Windows Host file for Traffic Redirect

Copy it on the Desktop. Now edit the file with any editor.   and add the some line like this.

127.0.0.1       localhost
127.0.0.1       mysite.com
127.0.0.1       mysite2.com
127.0.0.1       multisite.com
127.0.0.1       somesite.com

Note that you need to change it with your custom domain name. I am using mysite.com and mysite2.com as a example. So i added that site name here.

Custom Domain in XAMPP
Use Custom Domain Instead of LocalHost in XAMPP | Editing in Windows Host file for Traffic Redirect

After add domains name you chose earlier save it. Now copy and past the file here. Replace the file with it.

C:/Windows/System32/drivers/etc/hosts

Now final step you need to Restart Apache and test to make sure it is working. If you did everything that i showed you it will work. Now open the browser and  visit on your domain that we set mysite.com mysite2.com.

If you face any problems during this comment here and if you like this post please share this post.

Thank You!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.