Many developers have found themselves in this position before – you’re building a website for a client who is switching to a new host.  The idea is the install the new site on the new server, than change the nameservers in DNS and VOILA!  In 12-48 hours most people in the world will be directed to the new site when they try browsing to it.

The problem, then is how to install a site on the new server and make sure it’s working properly BEFORE editing the domain nameservers.  Thankfully, this is a very easy thing to do, thanks to the HOSTS file found in Windows, OS X & Linux.

What is the HOSTS file?

The HOSTS file is used in an operating system to map hostnames to IP addresses. (Wikipedia)

Hostname is another word for domain name.  A domain name combined with a top level domain (meaning .com or .org, etc) is what people enter into a browser to access a website – like www.echoleaf.com.

HOSTS files can be used to trick a computer into thinking that a website is located at a different IP address than it really is.  Viruses and malware sometimes target the HOSTS file to redirect victims to a site that looks like a legitimate site but in reality is not.  For example, they can try redirecting a victim to a fake Paypal or bank site to capture login information.  They also hope that the victim does not check the url because it will be a dead giveaway that the site is not real.  These urls are often long with lots of random letters and numbers after a legit-sounding beginning, like www.paypal.com.34534534.com/3987fh78ch7ygy7fg876sdf76

Editing the HOSTS file to point a domain to different IP address on your computer

Web developers can use this ability of the HOSTS files to trick their computer into thinking that a client’s website is located at a new IP address – the one of the new server.  The process goes like this:

  1. Find the IP address for the new hosting account or server.
  2. Find the HOSTS file
    • Windows
      • Go to C:\windows\system32\drivers\etc\ or %systemroot%\system32\drivers\etc\
      • In Windows XP, you can just double-click and start editing the file.
      • In Windows Vista or 7, click Start – search for Notepad, right-click and select Run as Administrator. From the Notepad file menu, open the HOSTS file, then start editing.
      • Saving: from the Notepad file menu, select Save.
    • OS X
      • Open Terminal
      • In Terminal, type in sudo nano /private/etc/hosts and hit Enter.
      • Enter your password as requested
      • Editing is done in the Terminal window, you can use arrow keys to navigate to the bottom of the file and add a new line.
      • Saving: Hit control-O to save the file.
    • Linux
  3. Add a new line mapping the client’s domain to its new IP address.  The format is like this: xxx.xxx.xxx.xxx www.domain.com
    • This line goes beneath all the other entries.  Make sure that line does not start with # as that will comment out the line and make it invisible to the operating system.
    • After editing, save the HOSTS file as noted above.
  4. Last step: clear the local DNS cache.  This must not be overlooked – if you do not clear the local DNS cache, your browsers will still think the website is located at the old IP address!
    • Windows
      • Click the Start button in lower left, select Run, then type cmd and hit Enter.
      • At the command prompt, type ipconfig /flushdns and hit Enter
      • Done!
    • OS X (10.5+)
      • Open Terminal
      • In Terminal, type in dscacheutil -flushcache and hit Enter
      • Done!
Now when you enter the client’s domain into your browser, you will see it at the new IP address.  You can install, configure & test BEFORE editing the DNS nameservers.  Keep in mind that this applies ONLY to the one computer whose HOSTS file was edited.  If you have 2 computers, you can edit the HOSTS file on one and use the other computer to check the old website located on the old server.
Hope this helps!