我有一个运行几个网站的IIS7 Web服务器。 有些网站是一个域的子域,其他域是完全独立的域。 我想使用IIS重写将一个域的所有子域网站redirect到https,但我希望其他域保持原样。 例如,我在同一个Web服务器上有以下站点:
one.test.com,two.test.com,otherdomain.com
我想设置一个全局的IIS重写,将http://one.test.com和http://two.test.comredirect到https,但otherdomain.com不应受到影响。
这是我到目前为止,当我testing了正则expression式似乎是正确的,但它不是redirect的子域名网站:
<rewrite> <globalRules> <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true"> <match url="(.*)(\.test\.com)" /> <conditions logicalGrouping="MatchAny"> </conditions> <action type="Redirect" url="https://{R1}{R2}" redirectType="SeeOther" /> </rule> </globalRules> </rewrite>
我是否过分复杂或缺less明显的东西?
干杯。
您需要将符合HTTP_HOST的条件添加到您的规则(URL重写中的“url”variables不包含主机名)。
<globalRules> <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="(.+)\.test\.com" /> </conditions> <action type="Redirect" url="https://{C:0}/{R:0}" /> </rule> </globalRules>
此规则应该将* .test.com上的所有请求redirect到HTTPS。
您必须在上述解决scheme中添加此条件<add input="{HTTPS}" pattern="off" /> 。 否则它将以循环结束。 所以规则如下,
<globalRules> <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="off" /> <add input="{HTTP_HOST}" pattern="(.+)\.test\.com" /> </conditions> <action type="Redirect" url="https://{C:0}/{R:0}" /> </rule> </globalRules>