Have you ever been working on a website, changed your DNS settings over to a different server and later needed to access that server again from the old domain name for some reason? Well if you are an active developer, this situation is somewhat common. I'll explain or you can skip the rest of this paragraph to quickly learn now to do it. Let's say you own the domain name example.com and a web server with an IP address of 22.214.171.124. Now assume you have a WordPress blog on that server that you had to move to another server with IP 126.96.36.199. Let's say you already changed the DNS settings for domain.com to point to the new server with IP 188.8.131.52 but you need to go back to the original WordPress site on the other server with an IP of 184.108.40.206. What do you do? We all know a WordPress site won't function properly with just the IP address, so that is out. What you need to do is repoint example.com to 220.127.116.11 in order to access that WordPress site again. What a PITA, right? Well read on and I'll show you a fast and easy way to make the site on the original server work with example.com even after you've pointed it to another IP address or web server! It's as simply as controlling a local host file on your local PC to make example.com route to 18.104.22.168 even though the internet routes it to 22.214.171.124! Here's how:
Using hosts file to override DNS settings for your PC
A lot of people don't realize that when you make a request to the Internet using your local computer it first checks a local copy of the hosts file for an entry and only if one isn’t present it goes out to the Internet DNS servers. Therefore there's an opportunity present for you to redirect example.com only for your own PC if you wanted to! Here are the easy steps:
- Open file explorer and navigate to C:\Windows\System32\drivers\etc.
- Open the file named "hosts" in notepad or another text editor that could be used as a code editor such as Notepad++, which is what I use.
- Now simply add a line to the end of the hosts file that contains the IP address of the server you want to route the domain name too followed by a space and then the domain name you want to reroute. So in our example scenario above, you would enter a new line that reads simply: 126.96.36.199 example.com
- Save the hosts file and open your browser and navigate to the domain which in our example was example.com. Note that there is a difference between example.com and www.example.com, so if you want it to work with www, you have to add another entry for www.example.com.
If you're using Notepad++ or similar as I was, you'll need to open it in administrator mode in order to be able to save the hosts file. Good luck! That's all there is to it.
How to change local DNS settings on a Mac
If you're on a Mac, the instructions are basically the same but do this instead:
From the terminal, type:
and then add the IP and domain name as described above, so the only real difference between Mac and a PC when it comes to changing DNS settings is that you will use a different editor and the hosts file is located in different places.