使用IIS 7.5 URL重写,如何将所有的请求不是从一个特定的子域redirect到一个特定的页面?

我想使用IIS 7.5中的URL重写模块将所有请求不是从特定的子域(x.domain.com)redirect到特定的文件夹/文件。

例如,这些应该按原样工作:

x.domain.com x.domain.com/asdf 

而像这样的东西:

 y.domain.com y.domain.com/asdf domain.com domain.com/asdf 

应该redirect到这样一个特定的页面(确切的URL,不依赖于使用的子域):

 domain.com/a 

不幸的是,我不能正确地configuration规则,因为它正确匹配,大部分时间,当它只是导致redirect循环。 (我知道我应该把我现在设置的错误规则,但他们甚至不会一直导致redirect循环。)

在IIS中设置与www.domain.com和domain.com相匹配的另一个Web站点是一个简单的解决scheme,但是我宁愿有一个网站来处理这些问题并redirect。

什么是正确的设置来获取此行为(使用用户界面或直接添加到Web.config)。

谢谢!

既然你还没有发布你现有的规则(导致redirect循环的规则),我不能告诉你为什么它不起作用。 我可以告诉你应该怎样工作:

 <rule name="Rewrite all but one subdomain" stopProcessing="true"> <match url="domain.com" /> <conditions logicalGrouping="matchAll">   <add input="{HTTP_HOST}" negate="true" pattern="^x.domain\.com$" /> <add input="{HTTP_HOST}" negate="true" pattern="^domain\.com$" /> </conditions> <action type="Redirect" url="http://domain.com/a/" appendQueryString="false" /> </rule> <rule name="Rewrite domain requests" stopProcessing="true"> <match url="domain.com" /> <conditions logicalGrouping="matchAll"> <add input="{HTTP_HOST}" pattern="^domain\.com$" /> <add input="{PATH_INFO}" pattern="^/a/$" negate="true" /> </conditions> <action type="Redirect" url="http://domain.com/a/" appendQueryString="false" /> </rule> 

在这里,我们必须规则,一个重写URL匹配domain.com所有请求,除了主机头完全匹配x.domain.comdomain.com (为了避免循环)。

第二条规则将匹配domain.com和除/a/之外的其他任何地方的请求,并在必要时redirect

Mathias的回答让我得到了我所需要的。 我不得不放弃的确切规则在下面,只涉及两个变化:

匹配的URL实际上是基于path,所以我不得不改变,以匹配任何东西(或没有),这是好的,因为IIS绑定正确的站点上的设置和条件将正确捕获它。

logicalGrouping的枚举需要是MatchAll。

 <rule name="Rewrite all but one subdomain" enabled="true" stopProcessing="true"> <match url=".?" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^x.domain\.com$" negate="true" /> <add input="{HTTP_HOST}" pattern="^domain\.com$" negate="true" /> </conditions> <action type="Redirect" url="http://domain.com/a/" appendQueryString="false" /> </rule> <rule name="Rewrite domain requests" stopProcessing="true"> <match url=".?" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^domain\.com$" /> <add input="{PATH_INFO}" pattern="^/a/$" negate="true" /> </conditions> <action type="Redirect" url="http://domain.com/a/" appendQueryString="false" /> </rule>