HTTP redirection issue in IIS, keep getting ERR_TO

2019-07-24 16:49发布

问题:

I am trying to enable HTTPS on a website on IIS. I want to redirect the user from http to https.

I have updated the rule accordingly in web.config to

<rewrite>
        <rules>
            <rule name="Redirect to HTTPs" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}/" redirectType="Permanent" />
            </rule>
        </rules>
</rewrite>

I am able to convert the request to https but then it keeps redirecting to the same url with https. The problem is that the rule apparently keeps redirecting all urls including to https and then the browser in the network tab keeps lots of 301 and eventually throws

This webpage has a redirect loop

ERR_TOO_MANY_REDIRECTS

Please help me out here if someone has tackled a similar situation. I can provide more information if necessary.

Thank you

回答1:

Try the following rewrite rule. This will work for websites served by single IIS server. Load balanced environments or server firms need some tweaks.

<rewrite>
    <rules>
        <rule name="Redirect to HTTPs" enabled="true" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="OFF" />
            </conditions>
            <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
    </rules>
</rewrite>

You can also have a look at the question and solution here