IIS 仅针对一个域的子域从 http 重写为 https

Cha*_*tte 5 iis rewrite iis-7 web-server

我有一个运行多个站点的 IIS7 Web 服务器。一些站点是一个域的子域,而其他站点则是完全独立的域。我想使用 IIS 重写将一个域的所有子域站点重定向到 https,但我希望其他域保持原样。例如,我在同一 Web 服务器上有以下站点:

one.test.com, two.test.com, otherdomain.com

我想设置一个全局 IIS 重写以将http://one.test.comhttp://two.test.com重定向到 https,但 otherdomain.com 不应该受到影响。

这是我到目前为止所拥有的,当我测试正则表达式时,它似乎是正确的,但它没有重定向子域站点:

<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>
Run Code Online (Sandbox Code Playgroud)

我是否过于复杂化或遗漏了一些明显的东西?

干杯。

小智 7

您需要添加与 HTTP_HOST 匹配的条件到您的规则(URL 重写中的“url”变量不包括主机名)。

<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>
Run Code Online (Sandbox Code Playgroud)

此规则应将 *.test.com 上的所有请求重定向到 HTTPS。