Home Tutorials How to change localhost to domain name for XAMPP

How to change localhost to domain name for XAMPP

40
0
SHARE

If you have some knowledge related to websites or web development as a whole, then you might have come across the word localhost which refers to your own PC. But, did you ever wondered how you can change localhost to a domain name for XAMPP or WAMP? For those who don’t know what XAMPP/ WAMP is? These are basically software that provides the tech stack for website development, namely PHP, MySQL, Apache, PhpMyAdmin, etc.

What is localhost?

Localhost is a hostname that refers to the current computer that is accessing it. In short, Localhost refers to your computer. It is used to access the network services that are running on the host (your computer) via the loopback network interface. For example, a locally hosted website can be accessed from a web browser by the URL  http://localhost  to display its homepage.

So today in this tutorial, I am going to show you how to change localhost to domain name?

How to change localhost to domain name for XAMPP?

Advertisement

Below is the step-by-step guide to change localhost to domain name for XAMPP, in a Windows computing environment.

Let’s, break the tutorial into 3 different parts:

  1. Point the custom domain to 127.0.0.1 (localhost)
  2. Add virtual hosts in apache server httpd-vhosts.conf file
  3. Run the server and test our work

Point the custom domain to 127.0.0.1 (localhost)

Now you have to add your custom domain and point it to the IP address 127.0.0.1 , which is the localhost IP address.

1. Goto to Search >> Search Notepad >> Run as Administrator

change localhost to domain name for XAMPP

2. On Notepad window, select File >> Open

3. Navigate to C:\Windows\System32\drivers\etc and set file types from “Text Document” to “All Files”

change localhost to domain name for XAMPP

4. Open the hosts file. Remember the file has no extension

5. Add your custom domain, as mentioned in the example below.

Example:    127.0.0.1  mydomain.com

change localhost to domain name for XAMPP

Here, localhost.com is the custom domain and it could be anything.

Now if you are not using XAMPP, and doing it for other backend technologies like Node.js, Django, etc. Just add that line in the host file and you are done. Just visit your domain and port, in this case, it is mydomain.com or mydomain.com:3000.

You are done. Now, mydomain.com will be redirected to the localhost.

However, If you want to add a custom domain alongside “localhost”. Follow the rest of the tutorial.

Add virtual hosts in apache server httpd-vhosts.conf file

  1. Launch Notepad >> File > Open
  2. Navigate to C:\xampp\apache\conf\extra. Make sure you navigate to the correct directory, where you installed XAMPP.
  3. Open httpd-vhosts.conf and Edit.
  4. Add the below line of codes.
## This for the localhost 
<VirtualHost *:80> 
   DocumentRoot "C:\xampp\htdocs\mydomain" 
   ServerName mydomain.com 
   ServerAlias mydomain.com 
      <Directory "C:\xampp\htdocs\mydomain"> 
         Require all granted 
      </Directory> 
</VirtualHost> 

## This is for the custom domain 
<VirtualHost *:80> 
  DocumentRoot "C:\xampp\htdocs\mydomain" 
  ServerName mydomain.com 
  ServerAlias mydomain.com 
     <Directory "C:\xampp\htdocs\mydomain"> 
        Require all granted 
     </Directory> 
</VirtualHost>

change localhost to domain name for XAMPP

Now restart your apache server and test it by visiting your domain custom domain you set up. If you followed the steps carefully, you should not face any errors.

If in case you are facing some error, please write them down below in the comments.

Advertisement

LEAVE A REPLY

Please enter your comment!
Please enter your name here