IIS URL将https非www重写为https-www

时间:2018-11-15 22:09:45

标签: asp.net iis url-rewriting web-config

配置重写规则非常困难。

我基本上想要:

  1. 非www要求使用www重定向到一个
  2. 非https请求应通过https重定向到一个请求

所需的重定向:

Requested Url                   Redirected To
http://example.com              https://www.example.com/
http://www.example.com          https://www.example.com/
https://example.com             https://www.example.com/
https://www.example.com         https://www.example.com/

重写配置1:

<rules>
    <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
        </conditions>
        <action type="Redirect" url="https://www.example.com/{R:0}" />
        <!--redirect to https -->
    </rule>
    <rule name="Add WWW prefix" stopProcessing="true">
        <match url=".*" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^example.com$" />
        </conditions>
        <action type="Redirect" url="https://www.example.com/" />
    </rule>         
</rules>

重写配置2 :(来自IIS Redirect non-www to www AND http to https)。

<rules>
    <rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAny">
            <add input="{HTTP_HOST}" pattern="^[^www]" />
            <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
</rules>

问题主要出在https://example.com上-定位不正确

1 个答案:

答案 0 :(得分:1)

尝试一下:

<rules>
    <rule name="Redirect example.com to www" patternSyntax="ECMAScript" stopProcessing="true">
        <match url=".*" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^example.com$" />
        </conditions>
        <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
    </rule>
    <rule name="Redirect to example.com https" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
        <match url="*" negate="false" />
        <conditions logicalGrouping="MatchAny">
            <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
    </rule>
</rules>