Testing your website with a hosts file

Stephen

Last Update 6 months ago

Testing your website with a local hosts file

A hosts file is used to map hostnames (ie. google.com) to IP addresses (ie. 216.58.199.78). You can change the IP to which you resolve a certain domain name by adding entries to your local hosts file on your computer. This is particularly useful when you wish to see how a website will look when hosted on a different server, without having to wait for a DNS change to propagate, or to avoid making any DNS changes entirely.


Many developers will opt to use a temporary URL to develop a website before they go live. This is usually so the developer can work on a website, without impacting the live site. However, developing a website on a temporary URL can cause problems when it comes time to go live. Many CMS and CRM applications require database manipulation to switch domains from temporary to live, which at times can break the website or application and make the switch-over process quite time-consuming.


However, by tricking your local machine into thinking that the new site is already sitting in its new location, you can test the site before updating DNS. This ensures that what you see before your site is live, is what you will see after you update the DNS.

The Problem

Missing assets (css files, javascript, images and other media) from your website can cause major site-rendering problems. Nearly every developer has come across an issue where a stylesheet isn’t loaded and the page looks like it has gone back to the late 90’s (a bunch of unformatted text with images splashed around or missing). Unfortunately, when using a temporary URL to develop, the website or application can create hard-coded links in the database that reference the assets using a fully qualified domain name (FQDN). Whether it be a CMS, CRM, theme or plugin creating these hard links, unless dealt with properly will cause your site to break when moving from a temporary URL to the production URL of a website.


Skilled system administrators and developers can use scripts or plugins to search and replace these in the database. Further to this, developers may even grep over the website contents in order to update these links, reflecting the correct production URL of the website. This can be time-consuming and some applications actually serialise data making it difficult or nearly impossible to locate and change the offending asset links.

The Solution

Developing the website by using its destination domain (the domain it will be accessed on when it is live) eliminates these problems. You can then modify your local hosts file to trick your local machine into thinking that DNS resolves to the IP Address of the server you have setup your account on. You can then develop and test in your browser as if the site were live. Once finished all you need to do is update the A records for the domains in DNS, at your DNS host for that domain.


Every Operating System has a local hosts file and depending on your Operating System will be located in a different place. So, in order to modify your hosts file, we have a couple of options below:


Linux / Mac OS X: /etc/hosts

Windows: %windir%\system32\drivers\etc\hosts


Editing the local hosts file is quite simple and all Operating Systems follow the same syntax. The IP Address you are masking followed by a space and then the hostname you want to masquerade. You can edit these with any text editor (although you may need to elevate your privileges in order to do so).


If an easy-to-use GUI is more your style, here are a couple of handy tools we recommend to use in order to make the job easier.


Helm (macOS) 

Helm is a local host file editing tool for macOS. Rather than having to use the terminal to elevate your privileges and then use a text editor to make the changes, Helm allows for rapid editing of your host file from within the GUI.

Hosts File Editor (Windows)

Hosts File Editor is an Open Source GUI-based local hosts file editing tool for Windows. The file permissions surrounding the hosts file on Windows are a bit harder to circumvent than they are in Linux or Mac OS X. This provides a simpler way for Windows users to make modifications to their hosts file and reduces the risk of corrupting the file.

Still need help? Message Us