Multiple localhost:80-bound sites in IIS?

21,511

Your hosts file needs to match your IIS bindings, which have to match the url browser.

So if your hosts file looks like:

127.0.0.1 siteA.localhost
127.0.0.1 siteB.localhost

Your IIS bindings need to be:

site    host header       port
siteA   siteA.localhost   80
siteB   siteB.localhost   80

Finally, from the local machine, you need to browse to:

http://siteA.localhost

EDIT - If you are trying to do the above without adding the hosts entries for siteA and siteB, it won't work. DNS won't find it. So for example, what happens if you try to browse to "foo.stackoverflow.com" - the request will fail even though "stackoverflow.com" is a known address.

Share:
21,511
Alpha
Author by

Alpha

var me = { from: "Argentina", passions: { music: "Electronic", job: "Software Architect" }, terribleSecret: function() { //TODO fix: they found me $(HiggsBoson).hide(); } };

Updated on May 19, 2020

Comments

  • Alpha
    Alpha about 4 years

    (You can skip directly to the question, but the context is on the background section.)

    Technical Context

    • Windows 8
    • IIS 7

    Background:

    As several others, I work with IIS and from time to time I need to set up a local version of a site. Now, in setting up different sites, it is quite common to just assign them different ports so that they don't overlap. So you could have your site A at port 80, your site B at port 81 and so on.

    Now, I want to setup the bindings so that I am able to have several sites under port 80 and with the header host distinction I can at the same time give meaningful URLs to these sites.

    An option to do this is with adding entries to your hosts file, such as:

    127.0.0.1 siteA.com
    127.0.0.1 siteB.com
    

    And with this, you should be able to browse to siteA.com or siteB.com and have that working locally.

    But I want to take one more step, as localhost is already set up to match 127.0.0.1, so:

    Question:

    I had set up the IIS bindings as:

    site    host header       port
    siteA   siteA.localhost   80
    siteB   siteB.localhost   80
    

    And I expected that I would be able to browse to http://siteA.localhost/ and http://siteB.localhost/ and get my sites.

    However, when I browse, it seems that my requests never reach IIS, and the address is not resolved.

    Why isn't that working?


    Update:

    I have chosen chue x's answer because he explained why this doesn't work. For those that may follow my same path, you may think "well, I could just add *.localhost in the hosts file and it should be done." or even switch to *.local or *.localdev.com or something of that sort.

    That, however, doesn't work: Wildcards in a Windows hosts file

    As chue x pointed out, our only approach right now is to keep adding entries to the hosts file or, how they explain in the linked question, to use another DNS server.

  • Alpha
    Alpha almost 11 years
    Since "localhost" is a known address -- is there any way I can avoid adding the entries to the host file? I'm trying to treat "localhost" as if it was a TLD or a full domain.
  • chue x
    chue x almost 11 years
    @Alpha - no because DNS cannot assume that "siteA.localhost" and "localhost" are the same address. They might be different addresses.
  • RenniePet
    RenniePet about 7 years
    Your hosts file is backwards, the IP address comes first.
  • chue x
    chue x about 7 years
    Thanks @RenniePet, fixed.