BlogTechnicalTesting your website with a local hosts file

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 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 temporary URL can cause problems when is 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 in to 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

Assets (css files, javascript, images and other media) when missing 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 hardcoded links in the database that reference the assets using a fully-qualified domain name.  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 it’s 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 to 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.

In order to modify your hosts file, we have a couple of options below.

Every Operating System has a local hosts file and depending on your Operating System, can be found in the following location:

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.

Gasmask (Mac OS X):

Gas Mask for OS X is a free GUI based local hosts file editing tool. Rather than having to use terminal to elevate your privileges and then use a text editor to make the changes, Gas Mask allows for rapid editing of your hosts file from within the GUI.

Gasmask Application

Gasmask Application

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 for a simpler way for Windows users to make modifications to their hosts file and reduces the risk of corrupting the file.

Hosts File Editor

Hosts File Editor