简体   繁体   中英

IIS Url rewrite - unsecured www to secured www

My site is published on IIS 8 (V.10.0.17), and I have added rules for redirecting to https.

All cases are covered:

  1. https://example.com -> https://example.com
  2. https://www.example.com -> https://example.com
  3. www.example.com -> https://example.com
  4. example.com -> https://example.com

except this one:

  1. http://www.example.com -> is not redirecting to : https://example.com

Not sure what am I missing, here is my web.confing:

<system.webServer>
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule1">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://example.com/{R:1}" />
                </rule>
                <rule name="HttpToHttps" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>

According to your description, I suggest you could try to use below url rewrite rule. You should use the example.com instead of the {HTTP_HOST}. Since the http_host will be the ww.example.com not example.com.

            <rule name="CanonicalHostNameRule1">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
                </conditions>
                <action type="Redirect" url="http://example.com/{R:1}" />
            </rule>
            <rule name="HttpToHttps" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://example.com/{R:1}" redirectType="SeeOther" />
            </rule>

Result

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM