Host File 101 – testing
One of your best testing tools for setting up a live and development site is the use of a local hosts file. We’ll explain a simple setup on a Window’s client, and point out how this is useful.
A hosts file basically maps an IP address to a DNS name, a host name. For example:
or even a subdomain too!
Pertaining to AWS, using the Public IP or EIP, and using a hosts file will allow you to enter mydomain.com into a browser and resolve without public DNS.
A host file is useful in a few situations. Primarly when you have a live site that is using mydomain.com and you need to toggle back and forth between live and dev. In this case, your hosts file will look something like this:
#184.108.40.206 mydomain.com #Live
220.127.116.11 mydomain.com #Dev
The active domain mapping is the one without the “#” in front as the above image shows.
Your browser should also be set to clear cache. Normally you can tell you are on Dev if you leave a self-signed certificate for HTTPS. This means you need to add an exception, and your URL for HTTPS will show a warning. 😉 Neat trick huh?
Additionally, a host file is useful as it bypasses a CDN/DNS setup like Cloudflare. So you’ll know that when you are testing you are seeing live results directly from the server, and not potentially cached stale results from the CDN.
For dealing with Hard-Coded URLs, a host file is invaluable when you need to test a site that is unfortunately using hard-coded URLs for the domain.
Used in this scenario, a host file can mimic domain structure without having the DNS made public.
And one more neat trick – staging. Let’s say I just deployed a Magento server, and I want to install, but either I don’t have access to DNS, or I just need to stage the server until I can change the DNS at a later time.
Adding a hostname mapping will allow me to proceed with the setup with, e.g.; https://example.com Wow! Two birds with one stone 😀 [Domain name gets set on install, and HTTPS gets set using a self-signed cert.]
Note: New versions of FireFox [78+] make use of DNS over HTTP. This feature must be disabled; otherwise, the host file will be by-passed.
Windows Hosts File Setup
Edit this file:
Add your entries, save, and then flush your browser cache, and if you have to flush the local DNS Window’s tables use:
Start >> Search >> CMD.exe
Launch CMD.exe and then enter:
Note: In some cases, even if your Window’s user is a local admin, you may need to add permission to the hosts file explicitly with file modify rights; otherwise, when you attempt to save your edits you will get an error.
Good to go!