Using the /etc/hosts file for custom domains during development

This document shows how to review a website with a custom domain name before the domain is publicly mapped to the web server. Lets assume your production/public website is at example.com. You would like to review the new version of the site using the example.com domain name but do not wish to have those changes seen by the general public.

This is accomplished by modifying the /etc/hosts file on your local computer to point the domain name to the IP Address of the development or staging web server you want to test. The /etc/hosts file contains a mappings of IP addresses to domain names that override what would normally be provided by DNS.

Editing your /etc/hosts file on a Mac

If you’re using a Mac with macOS, to edit your /etc/hosts file, open a Terminal window and run the following command:

sudo nano /private/etc/hosts

You may be asked for your password to edit the file. Enter your password.

Editing your /etc/hosts file using Windows

If you’re using Windows, to edit your \etc\hosts file, open %windir%\system32\drivers\etc\hosts and edit the file. If the directory and file don’t exist, you can create them.

Most Windows users may have trouble editing their \etc\hosts file because Windows includes file overwrite protections that allow only an Administrator to make changes to the file. To launch Notepad as Administrator, press the Windows key, type Notepad, right-click the Notepad application that appears, and select Run as administrator. (You can also launch any other text editor you prefer, such as Notepad++.)

Adding entries to the /etc/hosts file

The IP addresses for your website will have been provided to you. Add an entry to your /etc/hosts file in the following format:

[IP address]  [URL]

For example, if your Prod environment has the IP address 199.119.218.152 and the custom domain you want to use is www.example.com, add the following line to your /etc/hosts file:

199.119.218.152    example.com   www.example.com

With this change, whenever you visitexample.com or www.example.com, your browser will take you to your website hosted at the IP Address instead of website pointed to by the DNS record. You will likely need to completely close and restart your web browser for it to take effect.

When you  finished working with the development or staging website simply reverse these steps to restore normal browsing behavior.  If you need to switch between development/staging and production you may simply “comment out” the change by placing a # at the beginning of the line and saving the file:

#199.119.218.152    example.com   www.example.com