Is this possible to configure IIS7 to achieve the same functionality like Apache has regarding wildcard domains? I'm interested in routing user in ASP.NET web application based on subdomain user used in URL.
这是否可以配置IIS7以实现像Apache一样的关于通配符域的功能?我对基于URL中使用的子域用户在ASP.NET Web应用程序中路由用户感兴趣。
Something like is described here:
这里描述的东西如下:
http://steinsoft.net/index.php?site=programming/articles/apachewildcarddomain
Thanks
6 个解决方案
#1
The answer is No, IIS7 (still) does not support wildcard hostnames (see this). If you want to serve multiple domain on one website, the only workaround for now, as notandy suggested, is using a dedicated IP and doing it with DNS, which does support wildcards.
答案是否,IIS7(仍然)不支持通配符主机名(请参阅此内容)。如果你想在一个网站上提供多个域,那么现在唯一的解决方法就是使用专用的IP并使用DNS来支持通配符。
2013 Update
For completeness, IIS8 does not yet have this feature either.
为了完整起见,IIS8还没有此功能。
2016 Update
Finally, IIS 10 in Windows 2016 supports wildcard subdomains.
最后,Windows 2016中的IIS 10支持通配符子域。
#2
Does IIS support wildcard host header? Can I capture and redirect *.mydomain.com to one web site?
IIS是否支持通配符主机标头?我可以捕获* .mydomain.com并将其重定向到一个网站吗?
The answer is Yes/No. Yes, because you are able to redirect *.mydomain.com to one web site. No, because the magic is in DNS and not IIS.
答案是肯定/否定。是的,因为您可以将* .mydomain.com重定向到一个网站。不,因为魔法在DNS而不是IIS。
Here's how you do it:
At IIS MMC, configure a web site with NO host header, then assign an IP address to the site. (if you have one IP address in the box, then you can skip this). With this, the web site will bound to the specific IP and will listen to all HTTP requests send to the IP, and you are done :)
以下是您的操作方法:在IIS MMC中,配置一个没有主机头的网站,然后为该站点分配IP地址。 (如果框中有一个IP地址,则可以跳过此处)。有了这个,网站将绑定到特定的IP,并将监听发送到IP的所有HTTP请求,你就完成了:)
Next step is to make sure your name resolution works for the wildcard query and reply with the correct IP address. If you using Microsoft DNS service, it won't allow you to create a '*' A record (assuming you already created the domain zone in DNS MMC), you need to do the following:
下一步是确保您的名称解析适用于通配符查询并使用正确的IP地址进行回复。如果您使用Microsoft DNS服务,它将不允许您创建'*'A记录(假设您已在DNS MMC中创建了域区域),您需要执行以下操作:
- Navigate to
%windir%\system32\dns\
- Find the zone file. E.g.
mydomain.com.dns, open it with Notepad - Add an entry. E.g.
* A IP.IP.IP.IP
- Save the zone data file
- Reload the zone data in DNS MMC.
导航到%windir%\ system32 \ dns \
找到区域文件。例如。 mydomain.com.dns,用记事本打开它
添加条目。例如。 * A IP.IP.IP.IP
保存区域数据文件
在DNS MMC中重新加载区域数据。
Take note that by doing this, all * will response to the IP that you configured earlier. E.g. abc.mydomain.com, www.mydomain.com, K2k.mydomain.com and etc.
请注意,通过执行此操作,所有*将响应您之前配置的IP。例如。 abc.mydomain.com,www.mydomain.com,K2k.mydomain.com等。
To verify that it is working, try ping utility ping (insert anything here).mydomain.com
and you should get replies from IP.IP.IP.IP
要验证它是否正常工作,请尝试ping实用程序ping(在此处插入任何内容).mydomain.com,您应该收到来自IP.IP.IP.IP的回复
Then try browsing, http:// (insert anything here).mydomain.com/, you should get the same web page that you have configured.
然后尝试浏览,http://(在此处插入任何内容).mydomain.com /,您应该获得已配置的相同网页。
#3
You cannot create a wildcard (*) A record in Microsoft's DNS, but you can create a wildcard CNAME
. If all you are trying to do is direct all subdomains to a particular IP, this would work. For example, if you have an A record for www.mydomain.com
, you could add a CNAME
record for *.mydomain.com
and point that at www.mydomain.com
. The hostname in the request header will still be the subdomain, so your web app should be able to catch it and handle it if you want.
您无法在Microsoft的DNS中创建通配符(*)A记录,但您可以创建通配符CNAME。如果您要做的只是将所有子域指向特定的IP,这将起作用。例如,如果您有www.mydomain.com的A记录,则可以为* .mydomain.com添加CNAME记录,并将其指向www.mydomain.com。请求标头中的主机名仍然是子域,因此您的Web应用程序应该能够捕获它并根据需要处理它。
#4
You can use IIS if you use the Web Platform Installer (http://www.microsoft.com/web/downloads/platform.aspx) and install the "URL Rewrite" module. If you add an "A" record like "*.example.com", this allows all sub-domains to be sent to your IIS server. In the IIS server you simply add a URL rewrite rule to the default website to do whatever you wish, such as redirect to another URL. When a user enters, say, "xyz.example.com", you could redirect to "example.com", and point all unknown subdomains to the root domain. The steps are:
如果使用Web平台安装程序(http://www.microsoft.com/web/downloads/platform.aspx)并安装“URL重写”模块,则可以使用IIS。如果添加“A”记录(如“* .example.com”),则允许将所有子域发送到IIS服务器。在IIS服务器中,您只需将URL重写规则添加到默认网站即可执行任何操作,例如重定向到另一个URL。当用户输入“xyz.example.com”时,您可以重定向到“example.com”,并将所有未知子域指向根域。步骤是:
-
Install Web Platform Installer: http://www.microsoft.com/web/downloads/platform.aspx
安装Web平台安装程序:http://www.microsoft.com/web/downloads/platform.aspx
Run it, search for "URL Rewrite", and install it.
运行它,搜索“URL Rewrite”,然后安装它。
-
Go to the default website, and open "URL Rewrite".
转到默认网站,然后打开“URL Rewrite”。
-
Click "Add Rule(s)" on far right side.
点击最右侧的“添加规则”。
-
Name the rule and set the following:
命名规则并设置以下内容:
Requested URL: Matches the Pattern
请求的URL:匹配模式
Using: Regular Expressions
使用:正则表达式
Pattern: (.*)
Logical Grouping: Match All
逻辑分组:全部匹配
-
Add a new condition (click the "Add.." button), and set:
添加新条件(单击“添加...”按钮),然后设置:
Condition Input: {CACHE_URL}
条件输入:{CACHE_URL}
Check if input string: Matches the Pattern
检查输入字符串是否匹配模式
Pattern (modify as needed): (http|https)://(.*?)\.rootdomain\.tld
模式(根据需要修改):( http | https)://(。*?)\ .rootdomain \ .tld
Click [OK].
-
Now scroll down to action and set:
现在向下滚动到操作并设置:
Action Type: Redirect
操作类型:重定向
Redirect URL (modify as needed): {C:1}://rootdomain.tld{R:0}
重定向URL(根据需要进行修改):{C:1}://rootdomain.tld {R:0}
The "{C:#}" and "{R:#}" come from the test windows when you click the "Test pattern" buttons next to the regex pattern entries. You can match part of a server value and reuse it later.
当您单击正则表达式模式条目旁边的“测试模式”按钮时,“{C:#}”和“{R:#}”来自测试窗口。您可以匹配部分服务器值并在以后重复使用。
Also, you may want to checkout this tip on passing on the HTTP/HTTPS in the URL (the "{C:1}" in step 6 above): http://weblogs.asp.net/owscott/url-rewrite-protocol-http-https-in-the-action
此外,您可能希望在传递URL中的HTTP / HTTPS(上面的步骤6中的“{C:1}”)时查看此提示:http://weblogs.asp.net/owscott/url-rewrite-协议HTTP-HTTPS的内式行动
#5
Update 2: Link is broken, upgrade to IIS 10, it supports wildcards now.
更新2:链接中断,升级到IIS 10,它现在支持通配符。
UPDATE: This works for a while, after a while I became bombarded by multiple requests coming through. I would not recommend this method.
更新:这有一段时间了,过了一段时间我被多个请求轰炸了。我不推荐这种方法。
Make sure that your DNS records are setup correctly (CNAME record of *->@) and it will work flawlessly. Tested with multiple domains.
确保您的DNS记录设置正确(* - > @的CNAME记录),它将完美无缺地运行。经过多个域测试。
Link is broken now used to be http://pzxc.com/apache-style-wildcard-hostname-rewrite-rules-in-iis
链接已破坏,现在曾经是http://pzxc.com/apache-style-wildcard-hostname-rewrite-rules-in-iis
#6
For users who cannot upgrade to IIS 10+, it is not very difficult to enable wildcard host name support via reverse proxy.
对于无法升级到IIS 10+的用户,通过反向代理启用通配符主机名并不是很困难。
The HTTP example is as below, while other setup is just a few more steps.
HTTP示例如下,而其他设置只是几个步骤。
- Create a catch-all site which binds to *:80 and no host name.
- Create first site which binds to *:8091 and no host name.
- Create second site which binds to *:8092 and no host name.
- Test out both sites and move on.
- Install ARR and URL Rewrite module.
-
Create rewrite rules on the catch-all site to dispatch requests to the two sites,
在catch-all站点上创建重写规则以将请求分派给两个站点,
<system.webServer> <rewrite> <rules> <rule name="site1" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^(.*).site1.com$" /> </conditions> <action type="Rewrite" url="http://localhost:8091/{R:0}" /> </rule> <rule name="site2" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^(.*).site2.com$" /> </conditions> <action type="Rewrite" url="http://localhost:8092/{R:0}" /> </rule> </rules> </rewrite> </system.webServer>
创建一个绑定到*:80并且没有主机名的catch-all站点。
创建绑定到*:8091的第一个站点,没有主机名。
创建绑定到*:8092且没有主机名的第二个站点。
测试两个站点并继续前进。
安装ARR和URL重写模块。
Note that at server level, ARR proxy mode must be turned on,
请注意,在服务器级别,必须打开ARR代理模式,
<system.webServer>
<proxy enabled="true" />
</system.webServer>
IIS 10 wildcard host name is simpler and also has better performance, because ARR has its overhead (but generally speaking negligible).
IIS 10通配符主机名更简单,性能更好,因为ARR有其开销(但一般来说可以忽略不计)。
#1
The answer is No, IIS7 (still) does not support wildcard hostnames (see this). If you want to serve multiple domain on one website, the only workaround for now, as notandy suggested, is using a dedicated IP and doing it with DNS, which does support wildcards.
答案是否,IIS7(仍然)不支持通配符主机名(请参阅此内容)。如果你想在一个网站上提供多个域,那么现在唯一的解决方法就是使用专用的IP并使用DNS来支持通配符。
2013 Update
For completeness, IIS8 does not yet have this feature either.
为了完整起见,IIS8还没有此功能。
2016 Update
Finally, IIS 10 in Windows 2016 supports wildcard subdomains.
最后,Windows 2016中的IIS 10支持通配符子域。
#2
Does IIS support wildcard host header? Can I capture and redirect *.mydomain.com to one web site?
IIS是否支持通配符主机标头?我可以捕获* .mydomain.com并将其重定向到一个网站吗?
The answer is Yes/No. Yes, because you are able to redirect *.mydomain.com to one web site. No, because the magic is in DNS and not IIS.
答案是肯定/否定。是的,因为您可以将* .mydomain.com重定向到一个网站。不,因为魔法在DNS而不是IIS。
Here's how you do it:
At IIS MMC, configure a web site with NO host header, then assign an IP address to the site. (if you have one IP address in the box, then you can skip this). With this, the web site will bound to the specific IP and will listen to all HTTP requests send to the IP, and you are done :)
以下是您的操作方法:在IIS MMC中,配置一个没有主机头的网站,然后为该站点分配IP地址。 (如果框中有一个IP地址,则可以跳过此处)。有了这个,网站将绑定到特定的IP,并将监听发送到IP的所有HTTP请求,你就完成了:)
Next step is to make sure your name resolution works for the wildcard query and reply with the correct IP address. If you using Microsoft DNS service, it won't allow you to create a '*' A record (assuming you already created the domain zone in DNS MMC), you need to do the following:
下一步是确保您的名称解析适用于通配符查询并使用正确的IP地址进行回复。如果您使用Microsoft DNS服务,它将不允许您创建'*'A记录(假设您已在DNS MMC中创建了域区域),您需要执行以下操作:
- Navigate to
%windir%\system32\dns\
- Find the zone file. E.g.
mydomain.com.dns, open it with Notepad - Add an entry. E.g.
* A IP.IP.IP.IP
- Save the zone data file
- Reload the zone data in DNS MMC.
导航到%windir%\ system32 \ dns \
找到区域文件。例如。 mydomain.com.dns,用记事本打开它
添加条目。例如。 * A IP.IP.IP.IP
保存区域数据文件
在DNS MMC中重新加载区域数据。
Take note that by doing this, all * will response to the IP that you configured earlier. E.g. abc.mydomain.com, www.mydomain.com, K2k.mydomain.com and etc.
请注意,通过执行此操作,所有*将响应您之前配置的IP。例如。 abc.mydomain.com,www.mydomain.com,K2k.mydomain.com等。
To verify that it is working, try ping utility ping (insert anything here).mydomain.com
and you should get replies from IP.IP.IP.IP
要验证它是否正常工作,请尝试ping实用程序ping(在此处插入任何内容).mydomain.com,您应该收到来自IP.IP.IP.IP的回复
Then try browsing, http:// (insert anything here).mydomain.com/, you should get the same web page that you have configured.
然后尝试浏览,http://(在此处插入任何内容).mydomain.com /,您应该获得已配置的相同网页。
#3
You cannot create a wildcard (*) A record in Microsoft's DNS, but you can create a wildcard CNAME
. If all you are trying to do is direct all subdomains to a particular IP, this would work. For example, if you have an A record for www.mydomain.com
, you could add a CNAME
record for *.mydomain.com
and point that at www.mydomain.com
. The hostname in the request header will still be the subdomain, so your web app should be able to catch it and handle it if you want.
您无法在Microsoft的DNS中创建通配符(*)A记录,但您可以创建通配符CNAME。如果您要做的只是将所有子域指向特定的IP,这将起作用。例如,如果您有www.mydomain.com的A记录,则可以为* .mydomain.com添加CNAME记录,并将其指向www.mydomain.com。请求标头中的主机名仍然是子域,因此您的Web应用程序应该能够捕获它并根据需要处理它。
#4
You can use IIS if you use the Web Platform Installer (http://www.microsoft.com/web/downloads/platform.aspx) and install the "URL Rewrite" module. If you add an "A" record like "*.example.com", this allows all sub-domains to be sent to your IIS server. In the IIS server you simply add a URL rewrite rule to the default website to do whatever you wish, such as redirect to another URL. When a user enters, say, "xyz.example.com", you could redirect to "example.com", and point all unknown subdomains to the root domain. The steps are:
如果使用Web平台安装程序(http://www.microsoft.com/web/downloads/platform.aspx)并安装“URL重写”模块,则可以使用IIS。如果添加“A”记录(如“* .example.com”),则允许将所有子域发送到IIS服务器。在IIS服务器中,您只需将URL重写规则添加到默认网站即可执行任何操作,例如重定向到另一个URL。当用户输入“xyz.example.com”时,您可以重定向到“example.com”,并将所有未知子域指向根域。步骤是:
-
Install Web Platform Installer: http://www.microsoft.com/web/downloads/platform.aspx
安装Web平台安装程序:http://www.microsoft.com/web/downloads/platform.aspx
Run it, search for "URL Rewrite", and install it.
运行它,搜索“URL Rewrite”,然后安装它。
-
Go to the default website, and open "URL Rewrite".
转到默认网站,然后打开“URL Rewrite”。
-
Click "Add Rule(s)" on far right side.
点击最右侧的“添加规则”。
-
Name the rule and set the following:
命名规则并设置以下内容:
Requested URL: Matches the Pattern
请求的URL:匹配模式
Using: Regular Expressions
使用:正则表达式
Pattern: (.*)
Logical Grouping: Match All
逻辑分组:全部匹配
-
Add a new condition (click the "Add.." button), and set:
添加新条件(单击“添加...”按钮),然后设置:
Condition Input: {CACHE_URL}
条件输入:{CACHE_URL}
Check if input string: Matches the Pattern
检查输入字符串是否匹配模式
Pattern (modify as needed): (http|https)://(.*?)\.rootdomain\.tld
模式(根据需要修改):( http | https)://(。*?)\ .rootdomain \ .tld
Click [OK].
-
Now scroll down to action and set:
现在向下滚动到操作并设置:
Action Type: Redirect
操作类型:重定向
Redirect URL (modify as needed): {C:1}://rootdomain.tld{R:0}
重定向URL(根据需要进行修改):{C:1}://rootdomain.tld {R:0}
The "{C:#}" and "{R:#}" come from the test windows when you click the "Test pattern" buttons next to the regex pattern entries. You can match part of a server value and reuse it later.
当您单击正则表达式模式条目旁边的“测试模式”按钮时,“{C:#}”和“{R:#}”来自测试窗口。您可以匹配部分服务器值并在以后重复使用。
Also, you may want to checkout this tip on passing on the HTTP/HTTPS in the URL (the "{C:1}" in step 6 above): http://weblogs.asp.net/owscott/url-rewrite-protocol-http-https-in-the-action
此外,您可能希望在传递URL中的HTTP / HTTPS(上面的步骤6中的“{C:1}”)时查看此提示:http://weblogs.asp.net/owscott/url-rewrite-协议HTTP-HTTPS的内式行动
#5
Update 2: Link is broken, upgrade to IIS 10, it supports wildcards now.
更新2:链接中断,升级到IIS 10,它现在支持通配符。
UPDATE: This works for a while, after a while I became bombarded by multiple requests coming through. I would not recommend this method.
更新:这有一段时间了,过了一段时间我被多个请求轰炸了。我不推荐这种方法。
Make sure that your DNS records are setup correctly (CNAME record of *->@) and it will work flawlessly. Tested with multiple domains.
确保您的DNS记录设置正确(* - > @的CNAME记录),它将完美无缺地运行。经过多个域测试。
Link is broken now used to be http://pzxc.com/apache-style-wildcard-hostname-rewrite-rules-in-iis
链接已破坏,现在曾经是http://pzxc.com/apache-style-wildcard-hostname-rewrite-rules-in-iis
#6
For users who cannot upgrade to IIS 10+, it is not very difficult to enable wildcard host name support via reverse proxy.
对于无法升级到IIS 10+的用户,通过反向代理启用通配符主机名并不是很困难。
The HTTP example is as below, while other setup is just a few more steps.
HTTP示例如下,而其他设置只是几个步骤。
- Create a catch-all site which binds to *:80 and no host name.
- Create first site which binds to *:8091 and no host name.
- Create second site which binds to *:8092 and no host name.
- Test out both sites and move on.
- Install ARR and URL Rewrite module.
-
Create rewrite rules on the catch-all site to dispatch requests to the two sites,
在catch-all站点上创建重写规则以将请求分派给两个站点,
<system.webServer> <rewrite> <rules> <rule name="site1" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^(.*).site1.com$" /> </conditions> <action type="Rewrite" url="http://localhost:8091/{R:0}" /> </rule> <rule name="site2" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^(.*).site2.com$" /> </conditions> <action type="Rewrite" url="http://localhost:8092/{R:0}" /> </rule> </rules> </rewrite> </system.webServer>
创建一个绑定到*:80并且没有主机名的catch-all站点。
创建绑定到*:8091的第一个站点,没有主机名。
创建绑定到*:8092且没有主机名的第二个站点。
测试两个站点并继续前进。
安装ARR和URL重写模块。
Note that at server level, ARR proxy mode must be turned on,
请注意,在服务器级别,必须打开ARR代理模式,
<system.webServer>
<proxy enabled="true" />
</system.webServer>
IIS 10 wildcard host name is simpler and also has better performance, because ARR has its overhead (but generally speaking negligible).
IIS 10通配符主机名更简单,性能更好,因为ARR有其开销(但一般来说可以忽略不计)。