IIS Home @ it-notebook.org

Test host header configuration with the hosts file

(Kristofer Gafvert, July 25, 2006)

Introduction

Testing a website setup up in IIS and configured to use host headers can be problematic if a working name resolution is not in place, because the browser is highly dependent on name resolution to find the webserver (see Understanding Host Headers in IIS). The hosts file is a file located on each Windows machine and is used to resolve names to an IP address, and this file can be modified to resolve any (valid) name to an IP address, including any host header name you have set up for a website. This article will explain how to modify the hosts file to test a host header configuration for a website. This article will also be useful for developers working on multiple websites, but without a working external name resolution (for example the developer do not want to configure a DNS server).

Using the hosts file

The hosts file is located in the %SystemRoot%\system32\drivers\etc directory and can be opened with Notepad (the name of the file is hosts, without any extension). Since name resolution depends on a known IP address (you need to know what IP address the domain name should resolve to) you must know the IP address of the webserver. If working locally, you could use 127.0.0.1 as the IP address, but if you want to find out the real IP address of the machine follow these steps:

  • Click Start->Run and type cmd
  • Type ipconfig
  • The IP Address is written on the IP Address line

Once the IP address of the machine is known, and the host header name configured in IIS for the website is known, all the needed information to edit the hosts file is gathered.

  • Double-click the hosts file in Windows Explorer and select to open the file with Notepad.
  • After "localhost", press ENTER to start a new line and type the IP address followed by a blank character (tab for example) and then the domain name.
  • Save the file (make sure Notepad does not append an extension such as .txt)
Adding an entry in the hosts file

The ping command can be used to check that the changes in the hosts file are working. From a command prompt, type ping beta.gafvert.info (replace beta.gafvert.info with the name you have used).

The nslookup command cannot be used! nslookup does not use the hosts file, it uses a DNS server to resolve names.

Not a practical name resolution solution in large networks

Since the hosts file is stored on each client machine accessing the webserver it will become an administration nightmare to use it as a name resolution solution on a large network. In this situation DNS is a much better solution.

Resources

Understanding Host Headers in IIS
Using Host Headers to host multiple websites on IIS 6.0
Host Headers versus multiple IPs when hosting several websites
Can I use host headers to run multiple FTP sites?
Append a host header to a website in IIS by code