How to get multiple IIS web sites working on your development machine
I seems like many people get kind of stuck on the default web site and run everything under virtual directories beneath it on their dev machine which makes an environment that is not the same as a typical production web site which is usually a root level site, and it causes problems when they later want to move a site from their dev box to a production server.
It is actually very easy to setup additional root level web sites in IIS on your dev machine and use the hosts file to map fake host names for testing.
What Is a Host Name?
Its helpful to understand what the difference is between a host name and a domain name. When you purchase a domain name like mydomain.com, that is a host name, but it is considered the default host name for the domain because it has no host segment. With any domain name you can actually setup any number of host names like www.mydomain.com and foo.mydomain.com, and cheeseburger.mydomain.com are all valid host names you could setup for your domain. People sometimes call those sub-domains, but I think it is more helpful to understand that these are just different host names, a host is a computer and the domain name is like a top level name for a group of computers. The structure is basically hostname.domainname, so in the case of www.mydomain.com www is the host name portion and mydomain.com is the domain name portion and when they are put together that is a fully qualified host name, ie the host name is qualified by the domain name.
Historically there have been some conventions for naming hosts within a domain such as www.mydomain.com for the web server and mail.mydomain.com for the email server and so on, but these are just arbitrary, you can use any host name there is no rule about using www or not, its just a convention and people are kind of used to seeing www for web sites. But you can have mutiple web sites like web1.mydomain.com, web2.mydomain.com, test.mydomain.com etc.
Host names can map to different ip addreses or you can also have mutliple host names map to the same ip address. A machine can have one or more ip adddresses and therefore can have one or more host names that map to it.
Understanding How Names (Host Names) are Resolved To IP Addresses
Host names are resolved to ip addresses by DNS (Domain Name Servers), but before your machine checks the DNS servers it looks in its own hosts file and if it finds a mapping there it uses that instead of a DNS server. On a local network your IT dept may have setup a local DNS server to resolve names of machines on the local network to ip addresses on the local network. The Internet has a lot of DNS servers and typically the ISP that you use to connect to the internet assigns DNS Server(s) to your network card when it assigns you an ip address, and those DNS servers in conjunction with other DNS servers on the Internet resolve the names to ip addresses so your computer can connect to them.
But as we mentioned above, before checking with DNS servers your machine will look in its hosts file for a mapping for the host name.
On Windows the hosts file exists at C:\Windows\system32\drivers\etc folder
the file is named hosts and it has no file extension but it is just a text file. To edit it, you need to right click on Notepad and choose "Run as Administrator", then browse to C:\Windows\system32\drivers\etc
You'll need to change the file extension in Notepad to browse all files instead of .txt as shown here:
Then you can add your own custom host names there and map them to the loopback address 127.0.0.1
You'll see that is exactly how the hostname "localhost" works, it has a mapping in your hosts file.
You can add whatever host names you like and map them to an ip address by putting them on a single line for each host name following the ip address like this:
The thing to understand about the ip address 127.0.0.1 is that it is a special ip address on every machine known as the loopback address. For any given machine that address points to the machine itself. The loopback address doesn't even require that there is an actual network card on the machine. "localhost" always maps to the loopback address. In the above example we are just mapping additional arbitrary host names to the loopback address. If you have a network card with a fixed ip address you can just as easily map an arbitrary host name to that ip address instead of to 127.0.0.1. For example if your network card has an ip address of 192.168.0.100, you could put:
What Makes The Default Web Site "Default"
In IIS you can setup as many root level web sites as you want. Each site could be assigned a different ip address and therefore different host names mapped to those ip addresses could be handled by the corresponing IIS web site that has the ip address assigned to it.
However as mentioned above, multiple host names could all map to the same ip address. In order to be able to run mutliple web sites with different host names all using the same ip address, IIS has a way to assign a host name to a web site. This way it knows which web site to map the request to even though mutliple IIS web sites share the same ip address. In fact you don't even need to assign a specific ip address to a web site, you can leave it as "All Unassigned" and just add a host name, and when requests come in for that host name it will be mapped by IIS to the correct root level IIS Web Site. In IIS when you click the web site node then click the "Bindings" link on the right side of the page it opens a dialog where you can assign a host name and/or and ip address.
Note that for local testing host names can be arbitrary, they don't need to have a domain name portion, so it can be a single segment like mojotest1 as shown above, or you can use segments like www.fake.com if you prefer or www.fake.home, or fake.home.
Now what makes the Default Web Site "Default" is that it has no ip addreses nor host names assigned to it, so any request that is not mapped to another web site by host name or ip address will be handled by the Default Web Site. That is the only thing that makes a Default Web Site "Default". If you assign a host name to the site then it will no longer really be the "Default" even if it is named Default Web Site. There is nothing special about the Default Web Site other that it doesn't have any specific host names or ip addresses assigned to it. You could easily delete that site and make a new site with no ip or host name mapping and that new site would now be the default web site. The name of the site does not matter and is not what makes it the "default". The name of the IIS Web Site is not related at all to the host name or domain name. You could name it corresponding to the host name or domain name but that is just for display in IIS, it has no meaning or importance to the working of the site.
To add a new Web Site in IIS it must have a specific ip address or it must have a host name mapping. If you try to create a new Web Site without specifying an ip address or a host name you are in effect trying to make a second default web site and that won't work, one of them will work and the other one will not run at all, it will just hang (actually they both may hang if you do that). This is probably where people usually get stuck working with Default Web Site becuase they can't get another web site working, then they follow the path of least resistance and just setup "Sites" as Virtual Directories below the Default Web Site. But those are not really "Sites", only root level Web Sites are sites.
So when you create a new Web Site in IIS, you add a host name to the IIS web site and requests for that host name will be resolved to the IIS web site where you assigned it, instead of by the Default Web Site.
Created 2011-10-26 by Joe Audette
Updated 2012-02-09 by Joe Audette