Search code examples
asp.neturlhttpsurl-rewritingiis-8

IIS 8 - HTTP to HTTPS URL rewrite not working - ASP.NET site - Document moved


I have IIS 8 installed on Windows Server 2012 R2

Followed these steps to configure http to https so when clicking on http://a5.example.com, user is redirected to https://a5.example.com

but http to https redirection isn't working. disabled Require SSL on IIS

web.config:

<system.webServer>
        <rewrite>
            <rules>
                <rule name="Http to HTTPS" enabled="true" stopProcessing="true">
                    <match url="&quot;(.*)&quot;" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>

If i remove quotes in pattern

<match url="(.*)" />

getting error when typing http://a5.example.com

enter image description here

Tried these solutions and these but nothing helps

Updates after @JennyDai's answer:

Currently unable to change source code (set AllowAutoRedirect)

Enabled ARR proxy

enter image description here

but as soon as in web.config i specify

<match url="(.*)" />

and going to http://a5.example.com same error as in the above picture appears (document moved), https://a5.example.com/ws/services.asmx is available

enter image description here

when set <match url="&quot;(.*)&quot;" /> no errors and http to https doesn't work.


Solution

  • Found an answer, no need to install Application request routing, just needed to modify URL rewrite rule:

    <rule name="Http to HTTPS" enabled="false" stopProcessing="true">
                        <match url="(.*)" negate="false" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{HTTPS}" pattern="^OFF$" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
                    </rule>
    

    Website relies on the webservice and was configured to talk to it (the /ws/Services.asmx url) on http://, so when the website made a call to the webservice with http:// the request got caught by the https redirect rule which sent it a redirect response header (301 found message). So the original POST call got a 301 response to redirect it to the https:// url which was not what the website expected so the webservice call (the POST) essentially got lost and it was as if the webservice was unreachable causing the error message to appear.

    The url to the webservice was configurable in the web.config file so the website is now calling the webservice directly using https:// and no ARR was needed