Hosts File - Testing New Web Servers Before Changing DNS Settings

published 14.Oct.2004

Introduction

If your moving your web site from one server to another, or moving from one hosting provider to another, you need a way to check your site is working on the new server before instructing your domain registrar to change the DNS records to point the domain to your new server. Some providers give you a preview URL which is fine up to a point, but there is a better way which allows you to make your computer point your domain to the new server temporarily.

Enter the Hosts File

The Hosts file is a simple text file which is found in all operating systems which can hold a list of IP addresses and domain names. Typically when you browse to a web site, your computer checks the DNS entry for your domain and returns the IP address. So for example when you visit www.google.com the DNS servers return the IP address of 66.102.9.99

Any IP address and domain name reference you add to your hosts file will overide what the main DNS servers report, as it is local to your machine. You can demonstrate this by locating your hosts file (see below) and adding a single line like :-

216.109.118.79 www.google.com

At the time of writing this tutorial 216.109.118.79 is an IP address for Yahoo.com, so what we are basically doing is mapping google.com to an IP address owned by Yahoo. After saving the changes to the hosts file, try browsing to www.google.com and you should get Yahoos home page.

So you can see how easy it is to enter the IP address of your new server into in the hosts file associating it with your domain, and you can fully test your web site before having the DNS changed.

Location of the Hosts file on different Operating Systems

Linux / Unix = /etc/hosts

Windows 3.1/95/98ME = c:\windows\hosts

Windows NT/2000/XP Pro = c:\winnt\system32\drivers\etc\hosts or c:\windows\system32\drivers\etc\hosts

Windows XP Home = c:\windows\system32\drivers\etc\hosts

Netware = SYS:ETC/HOSTS

Apple = System Folder:Preferences and in the System Folder itself

If your using a Windows machine you may not have a hosts file. If this is the case there will be a sample host file in the location identified above called hosts.sam, which you can open in notepad and save as hosts (no extension).

Editing the Hosts File

The Hosts file is a simple text file which can be edited with your favourate text editor. When inputting entries in the hosts file ensure there is at least one space between the IP address and the domain name. Each entry should be on a separate line. Do not use any web notations such as \, /, or http:// just enter the domain name including the three w's.

You can disable a particular entry by putting a # character in front of that line.

Using the Hosts File to Block Ads, Banners, Cookies and Web Bugs

You can use a Hosts file for other things like blocking ads, banners, cookies, web bugs, and even most hijackers. This is accomplished by blocking the server that supplies these little gems. For example an entry like :-

127.0.0.1 ad.doubleclick.net

blocks all files supplied by the DoubleClick server to the web page you are viewing, by effectively mapping the domain to your computer (127.0.0.1). This also prevents the server from tracking your movements.

The MVPS Host File can be downloaded which already contains a large list of known ad servers, banner sites and sites that give tracking cookies.

What Next?

Bookmark this article at :-

 

 

About Author:
I'm a father, husband, and a software developer who works for the NHS. more »

Sections :
« Articles
« Contact


 

EditPlus Text Editor - an Internet-ready 32-bit text editor, with syntax highlighting for HTML, PHP, CSS and more.