简体   繁体   English

如何以编程方式为IIS7创建子域?

[英]How to create subdomains for IIS7 programmatically?

I'm writing a SaaS app in C#/ASP.NET, hosted with IIS7. 我正在用IIS7托管的C#/ ASP.NET中编写一个SaaS应用程序。 I want to create a personalized subdomain for every customer that signs up, ie fred.mydomain.com, bob.mydomain.com, each of which will point to the same app, just with a different skin per customer. 我想为每个注册的客户创建一个个性化的子域名,即fred.mydomain.com,bob.mydomain.com,每个客户都指向同一个应用程序,每个客户只有不同的皮肤。

How do I create these subdomains programmatically? 如何以编程方式创建这些子域?

Use URL Rewrite for IIS7 to map all requests like user.mydomain.com (where user is not www, mail or other existing real subdomains) to mydomain.com/myapp?id=user Then in the script handle whatever theming you need. 使用IIS 重写 user.mydomain.com将所有请求(如user.mydomain.com (用户不是www,邮件或其他现有的真实子域))映射到mydomain.com/myapp?id=user然后在脚本句柄中处理您需要的任何主题。

You do not need to add rule for every user created. 您无需为每个创建的用户添加规则。 Just create one general rule to do so. 只需创建一个通用规则即可。

And, also, in your server DNS, you need to forward *.mydomain.com (where * is not www, mail or other existing real subdomains) to mydomain.com IP. 而且,在您的服务器DNS中,您需要将* .mydomain.com(其中*不是www,邮件或其他现有的真实子域)转发到mydomain.com IP。 This is pretty straight forward. 这很简单。 You already have DNS records for existing subdomains. 您已拥有现有子域的DNS记录。 Just add *.mydomain.com and point to mydomain.com. 只需添加* .mydomain.com并指向mydomain.com即可。 This will do the DNS part of the trick. 这将是DNS技巧的一部分。 Other part is in the URL Rewrite 其他部分在URL Rewrite中

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. 设置IIS,以便所有传入请求(无论主机标头)指向此应用程序。 All sites have to either have a unique hostname or unique port in IIS, so you would set this up by: 所有站点都必须在IIS中具有唯一的主机名或唯一端口,因此您可以通过以下方式设置:

    1. Binding the site to the default port of 80. 将站点绑定到默认端口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. 这也是首次安装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. 找出服务器的静态IP地址,并告诉每个注册的新客户端将其域的DNS指向该IP。 Or, if you will own the domain name, setup a catchall DNS entry: *.mydomain.com - points to the IP address of your server. 或者,如果您将拥有域名,请设置一个catchall DNS条目:* .mydomain.com - 指向您的服务器的IP地址。

  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. 从那里,您可以根据一组数据库值测试其值,您必须根据该URL确定需要加载哪个MasterPage / css样式表/等。 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. 请记住,如果你像这样做了一个catchall,你需要考虑一个错误的URL,因此你没有皮肤可以匹配它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM