Sometimes we need to change host files in windows because the domain name is not publicly pointing to the correct server.
So how does it work?
When we type and go to a domain name in browser, let’s say codinghelphq.com, your browser does not reach the server right away. There are the (simplified) steps:
- the browser will seek for the DNS information on the domain (usually through public sources)
- the DNS information will contain an IP address, which is the precise “address” for the server
- the browser contacts the IP address, requesting for the website.
So you can see if #1 – the DNS information is not available or not up-to-date or wrong, then the browser will never get to the correct server.
The DNS information is usually not wrong, but sometimes it is. For example, if the domain owners just updated the DNS information / change server, usually it takes hours or even half a day for new / correct information to be shown publicly. (The process is called DNS propagation) So during this time, any one trying to go onto the website will get an error.
So when we do web development, sometimes we will want to work on a website before the domain settings are correct. (Don’t want to wait!). To bypass this, we can give the computer direct instructions on what IP address to look when trying to go on a domain name. We do this by editing the host file to override public information. Any change will only affect our own computer. Other people will not be affected.
The above literally means if user want to go to “somedomainexample.com” , the browser shall contact IP address 126.96.36.199
(your browser will not use public DNS information, but will use your override instead.)
Steps on how to edit hosts file on Windows
- Search for notepad
- Right click on Notepad on the search result, then choose to run as administrator
- Once notepad is run as administrator, open up the file using the c:\windows\system32\drivers\etc\hosts
- Update the host file, add on the IP address and website address
- Click Save
This does not work, how do I fix?
If the host file is edited but still does not work, it is likely due to DNS cache. Your computer AND browser saves old DNS information and re-use it so it does not have to do a DNS look up every time. But this means you need to clear BOTH cache for this to work.
- Go to command prompt (dos), issue command – this clears computer dns cache
- ipconfig /flushdns
- Close ALL browser tabs and use private mode
- Or just use another browser that was not on website before (so old cache is not used)
- This clears browser dns cache
This should work 99% of the time