IIS URLRewrite https non-www to https-www

纵饮孤独 提交于 2021-02-11 12:32:04

问题


Having a tough time configuring rewrite rules.

I basically want :

  1. Non www requests to be redirected to one with www
  2. Non https requests should be redirected to one with https

Desired Redirects:

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/

Rewrite configuration 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>

Rewrite configuration 2: (from 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>

The issue is mainly with https://example.com - it's not directing properly


回答1:


Try this:

<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>


来源:https://stackoverflow.com/questions/53328593/iis-urlrewrite-https-non-www-to-https-www

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!