15

(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.

5
  • I think I see what you are saying. So you are trying to browse to siteA.localhost without adding an entry in the hosts file?
    – chue x
    May 30, 2013 at 15:26
  • @chuex Thank you very much. With your answer and some other findings I now understand why this doesn't work, and that what I was trying to achieve is not possible in this way.
    – Alpha
    May 30, 2013 at 22:32
  • possible duplicate of Forms Authentication across Sub-Domains on local IIS
    – Serj Sagan
    Nov 26, 2013 at 22:31
  • Your hosts file is backwards, the IP address comes first.
    – RenniePet
    May 18, 2017 at 4:30
  • @RenniePet Thanks! I probably wrote it down wrong at the time of writing the question. I fixed it just now.
    – Alpha
    May 18, 2017 at 14:13

1 Answer 1

21

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.

5
  • 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.
    – Alpha
    May 30, 2013 at 15:39
  • @Alpha - no because DNS cannot assume that "siteA.localhost" and "localhost" are the same address. They might be different addresses.
    – chue x
    May 30, 2013 at 15:54
  • Your hosts file is backwards, the IP address comes first.
    – RenniePet
    May 18, 2017 at 4:30
  • Thanks @RenniePet, fixed.
    – chue x
    May 18, 2017 at 13:26
  • Setting things up this way doesn't work for me...I get a login prompt as shown here. How can I get around this given that I am using Windows Authentication (Kerberos) only?
    – Kenny83
    Dec 7, 2023 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.