IIS重写 - 仅将一个主机名的根重定向到HTTPS?

时间:2016-05-04 20:40:36

标签: redirect iis https

我正在尝试仅将输入http://site.domain.com的用户重定向到https://site.domain.com

任何其他http页面都不应重定向到https(例如:http://site.domain.com/page2

我尝试过这种方法:

    <rewrite>
        <rules>
            <rule name="Redirect to HTTPS" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
                <match url="^$" ignoreCase="true" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTPS}" pattern="^OFF$" />
                    <add input="{HTTP_HOST}" pattern="^site\.domain\.com$" />
                </conditions>
                <action type="Redirect" url="https://site.domain.com/" appendQueryString="false" redirectType="SeeOther" />
            </rule>
        </rules>
    </rewrite>

或者这个:

    <rewrite>
        <rules>
            <rule name="Redirect to HTTPS" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
                <match url="(.*)" ignoreCase="true" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTPS}" pattern="^OFF$" />
                    <add input="{HTTP_HOST}" pattern="^site\.domain\.com$" />
                    <add input="{PATH_INFO}" pattern="^$" />
                </conditions>
                <action type="Redirect" url="https://site.domain.com/" appendQueryString="false" redirectType="SeeOther" />
            </rule>
        </rules>
    </rewrite>

但是没有重定向。我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

What is the regular expression to match the empty string for a rewrite rule?

但我会坚持使用^ $并尝试这个变种:

<rewrite>
    <rules>
        <rule name="Redirect to HTTPS" stopProcessing="false">
            <match url="^$" />
            <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            </conditions>
            <action type="Redirect" url="https://{SERVER_NAME}/" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>