Posts Tagged ‘DNS trick’

Using Windows’ “hosts” file to avoid downtime when switching web hosts

I do have a lot of people asking me what’s the best way to transfer their website from a web host to another without any downtime. While I think this is almost impossible (at least coherently and without anyone noticing it), there are a few tricks that you can use to minimize the downtime.

One of them is to use the Windows HOSTS file to prepare everything at the new web host.
The HOSTS file, is a basic text file that allows you to override your DNS server settings. By having a web address in there, you will make your PC ignore any DNS Resolution (it won’t even try to resolve the address) – Instead, it will use the IP Address you supply in the HOSTS file.

Think of that file as a “God Mode” in DNS.
Whatever you wrote there, will be the absolute path for a given domain name.

So, when you’re moving to a new web host this becomes handy, because you can simply make your PC resolve your URL to the new web hosts’ IP address, without having to change your domains’ nameservers. The advantage here is that you will be able to preview your site *exactly* as it will be functioning at the new location without risking a DNS change; along with its side-effects if it goes wrong (having to change it back to the previous host because it’s not working as expected, etc).

 

So let’s say you’re moving from WEBHOST1 to WEBHOST2.
The very first thing you must do is to find out your website’s new IP address.

This is an information that is usually sent to you by the new web hosting provider through a “Welcome Email” along with other useful technical notes about the new account. If this isn’t the case, simply ask your web host (WEBHOST2) what your IP address will be.

Once you have that information, you can move on to the next step – editing the HOSTS file.
By default, on a standard setup; the HOSTS file is located at the following location:

Windows 95, Windows 98 and Windows ME: C:\Windows\Hosts
Windows NT/2000: C:\WINNT\system32\drivers\etc\hosts
Windows XP: C:\Windows\system32\drivers\etc\hosts
Windows Vista and Windows 7: C:\Windows\System32\drivers\etc **

 

** Under Windows Vista and Windows 7, you will need to open this file using the option “Run as Administrator” in order to prevent issues when saving your file. (you won’t be able to save the modified file if you do not use this option)

Since this file has no extension, you will most likely be asked which program to use to deal with the file. Select NOTEPAD or WORDPAD as an alternative.
Once you open it, you should have the following (or at least similar) text:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#’ symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
 
127.0.0.1 localhost

 

On Windows Vista/Windows 7, you might be missing the 127.0.0.1 entry.
This is because both Operating Systems do not need such line there, the localhost entry is handled by the DNS client directly.

You will instead see a few extra lines replacing the first entry, such as:

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

Moving on, let’s say your domain name is “myblog.com” and your new web hosting IP address is 74.74.74.74 – where is the entries you would need to add to this file:

 

74.74.74.74 myblog.com

74.74.74.74 www.myblog.com

So, to summarize, you would have a file similar to this one:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#’ symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

74.74.74.74 myblog.com

74.74.74.74 www.myblog.com

This will order your PC to not even try to resolve the domain name, but to use the IP you specified instead. Once you save this file, your domain should start to point to the new IP immediately. If you want to make sure it’s happening, a good way to do it is by performing a PING test on your domain.

To do so, just open a new Command Prompt window (by going to Programs/All Programs –> Accessories –> Command Prompt), and typing the following command:

ping www.myblog.com

Then wait for the output. If you get the new IP address, that means you’re ready.
Upload your website to the new host, restore any databases you need to have working and test it on your PC. The best part is that no one will notice any flaws on the new host – only you will be able to see them, as the rest of the world will still be using the current/old host to retrieve your content.

Once you’re fully satisfied with what you see on your domain, you can safely change your nameservers (pointing them to the ones you have been provided by WEBHOST2) without risking anything – because you have tested it all beforehand.

IMPORTANT NOTE:  Once you are satisfied with the results and are ready to change the nameservers, make sure you remove those 2 lines from your HOSTS file. You will no longer need them, and having them there can cause problems in the future (for example if for any reason your web host has to change your IP address… This seems unlikely but it isn’t. There are many valid reasons to change your IP, such as installing a SSL Certificate!)