IIS 7 中 HTTP 和 HTTPS 的域重写

Domain Rewrite for both HTTP and HTTPS in IIS 7

我想在一个请求中进行多次 301 重定向,我发现 this article 很有用。但是,我有一个问题 http://example.com 重定向到 https://www.example.comhttps://example.com 没有重定向到 https://www.example.com

下面是我的示例:

  1. SEO - 删除 default.aspx(工作正常但没有 HTTPS)
    https://www.example.com/app/default.aspx => https://www.example.com/app/

但是http://www.example.com/app/default.aspx => http://www.example.com/app/

  1. SEO - 删除尾部斜线(不起作用,没有 HTTPS,如上所述)
    所有链接都改为使用尾部斜杠
    http://www.example.com/app => http://www.example.com/app/
    https://www.example.com/app => https://www.example.com/app/

  2. SEO - ToLower(工作正常但没有上面的 HTTPS)
    http://www.example.com/APP => http://www.example.com/app
    https://www.example.com/APP => https://www.example.com/app

  3. SEO - http 规范重定向(有效 100%)
    http://example.com/app => https://www.example.com/app

  4. SEO - https 规范重定向(根本不起作用)
    https://example.com/app => https://www.example.com/app - 失败

  5. SEO - 非规范重定向(100% 无效。如果没有此规则,样式表将无法加载,但与上述任何内容不匹配的其他链接也不会加载)
    https://example.com/app => https://example.com/app/
    http://example.com/app => http://example/com/app/
    https://www.example.com/app => https://www.example.com/app/
    http://www.example.com/app => http://www.example.com/app/

这是我实现的,问题如上

<rewrite>
    <rules>
        <clear />
        <rule name="WhiteList - resources" stopProcessing="true">
            <match url="^resources/" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="None" />
        </rule>
        <rule name="SEO - remove default.aspx" stopProcessing="false">
            <match url="(.*?)/?default\.aspx$" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_METHOD}" pattern="GET" />
            </conditions>
            <action type="Rewrite" url="_{R:1}" />
        </rule>
        <rule name="SEO - Remove trailing slash" stopProcessing="false">
            <match url="(.+)/$" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_METHOD}" pattern="GET" />
               <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Rewrite" url="_{R:1}" />
        </rule>
        <rule name="SEO - ToLower" stopProcessing="false">
            <match url="(.*)" ignoreCase="false" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_METHOD}" pattern="GET" />
                <add input="{R:1}" pattern="[A-Z]" ignoreCase="false" />
            </conditions>
            <action type="Rewrite" url="_{ToLower:{R:1}}" />
        </rule>
        <rule name="SEO - http canonical redirect" stopProcessing="true">
            <match url="^(_*)(.*)" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_HOST}" matchType="Pattern" pattern="^www\.example\.com$" ignoreCase="true" negate="true" />
                <add input="{HTTP_METHOD}" pattern="GET" />
            </conditions>
            <action type="Redirect" url="https://www.example.com/{R:2}" />
        </rule>
        <rule name="SEO - https canonical redirect" stopProcessing="true">
            <match url="^(_*)(.*)" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_HOST}" matchType="Pattern" pattern="^www\.example\.com$" ignoreCase="true" negate="true" />
                <add input="{HTTP_METHOD}" pattern="GET" />
                <add input="{HTTPS}" pattern="^ON$" ignoreCase="true" />
            </conditions>
            <action type="Redirect" url="https://www.example.com/{R:2}" />
        </rule>
        <rule name="SEO - non-canonical redirect" stopProcessing="true">
            <match url="^(_+)(.*)" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_METHOD}" pattern="GET" />
            </conditions>
            <action type="Redirect" url="{R:2}" />
        </rule>         
    </rules>
</rewrite>

这不是真正的解决方案,因为我发现了 IIS 的实际问题。

当您访问时 https://example.com with the rules above setup (or https canonical redirect) it will first give you a certificate error if you don't have the "naked domain" certification. If the user opt to continue, then the redirect to https://www.example.com 会发生。

我认为这可能发生在 "DNS Negotiation" 级别而不是 IIS 级别。