Testing Website by Modifying Hosts File

Web developers often come across challenge to test a website in browser after an update or upgrade before actually pointing to production site. There are few different ways to handle this:

Method 1

Use a third party site like https://hosts.cx Enter server IP and domain name. It will generate a temp url for you that you can browse through or share with others to take a look.

Limitation: It only works for static pages, for interactive pages, login areas this does not work well.
Also if the website is behind any firewall or proxy, it may not work.

Method 2

Another solution is by modifying hosts file of your local computer. When you modify hosts file with the setting as shown below, the browser on your local machine will load website from that IP or test server, not from production server. For example my website rightquestion.ca is hosted on webserver.com. For an update/upgrade to website and to test before making it live, I need to browse website. This is what I will do:

  1. Add an entry in my hosts file as 1.1.1.1 rightquestion.ca
    1.1.1.1 rightquestion.ca
  2. Flush dns cache by typing “ipconfig/flushdns” in windows command prompt as shown below
Flush DNS on Windows Command Prompt

Next time I load the website in browser, it will be from IP 1.1.1.1 and not from webserver.com which is still available as it is to the public.

However, there is usually an extra step you might have to do in case you have a https site.

HSTS Setting Chrome

Depending on what browser you are using, there are different ways to modify setting.

In Chrome address bar type chrome://net-internals/#hsts

Chrome HSTS settings

Step 1 Click on Domain Security Policy on left menu panel

Step 2 Enter your domain name in Query field and press Query button

Step 3 Scroll down, type domain name again in Delete field and press Delete

After steps above, you should be able to test the website without making dns change.

Leave a Reply

Your email address will not be published. Required fields are marked *