How to create subdomains for IIS7 programmatically?

后端 未结 2 481
清酒与你
清酒与你 2021-02-06 03:59

I\'m writing a SaaS app in C#/ASP.NET, hosted with IIS7. I want to create a personalized subdomain for every customer that signs up, i.e. fred.mydomain.com, bob.mydomain.com, e

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 04:42

    Realizing of course that someone already answered your question by telling you to do redirects, it seems the easier way might be to just grab the host server variable.

    1. Setup IIS so that all incoming requests (regardless of the host header) point to this one application. All sites have to either have a unique hostname or unique port in IIS, so you would set this up by:

      1. Binding the site to the default port of 80.

      2. Not providing anything in the Host Name field. This is also how the Default Website is setup by default when you first install IIS.

    2. Figure out the static IP address of your server, and tell each new client that signs up to point the DNS for their domain to that IP. Or, if you will own the domain name, setup a catchall DNS entry: *.mydomain.com - points to the IP address of your server.

    3. Within your application, check for the current host header in order to provide a different skin or master page.

    This should grab the host header from within the code:

      Request.ServerVariables["HTTP_HOST"]
    

    From there you could test its value against a set of database values you have to determine which MasterPage/css stylesheet/etc you need to load based on that URL. Keep in mind if you do a catchall like this that you'll need to account for a URL mistyped that you therefore wouldn't have a skin to match to it.

提交回复
热议问题