0
votes

Here is the broken piece of the rewrite rules

    <rules>
                    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                        </conditions>
                        <action type="Redirect" url="https://www.example.com/TEST_SITE/{R:1}"
                                redirectType="Permanent" />
                    </rule>
    </rules>

My intention is to test if redirect http to https works on my test site, however,it will be a infinity loop with code 301 (Moved Permanently)

If I change the url="https://www.example.com/TEST_SITE/{R:1} to url="{HTTP_HOST}/{R:1}", it will work, no more loop. But instead of redirecting me to https://www.example.com/TEST_SITE/, it redirects me to https://www.example.com/ (prod environment).

Any ideas to fix this ? Thanks

2
What happens if you change url="https://www.example.com/TEST_SITE/{R:1}" to url="https://{HTTP_HOST}/TEST_SITE/{R:1}"? There are more HTTP to HTTPS redirect examples on stackoverflow.com/a/14925644/1297898 and saotn.org/iis-url-rewrite-redirect-http-to-https. Sometimes it takes some fiddling :)Jan Reilink
@Jan Reilink url="https://{HTTP_HOST}/TEST_SITE/{R:1} I tried this one before, doesn't work . Thanks for your comments and links. I will look into it.uonlyYOLOonce
Where exactly is the rule defined ? At the example.com level or example.com/TEST_SITE level . See this blog blogs.msdn.microsoft.com/kaushal/2013/05/22/… it says - "NOTE: Ensure the rewrite rule is disabled at each of the virtual directories/applications under the Default Web Site. Due to inheritance, the rule will cause the requests to end up in infinite loop calling itself repeatedly. "Ravi A.
I fixed this issue after a while. I printed out all the server variable , it turns out the server variable {https} returns nothing and it returns an additional variable {http-x-SSL} to indicate if its SSL connections. Thank you alluonlyYOLOonce
Great one @uonlyYOLOonce ! Perhaps you can update your question with your solution? My best guess is that there is an URL Rewrite active on the load balancer ("lb") and/or traffic from the lb to the webservers is not SSL encrypted.Jan Reilink

2 Answers

0
votes

Stupid of me I didn't think of this yesterday... Add your domain name / HTTP_HOST as an input condition.

This comes from a real-life example from my site:

<!-- redirect HTTP naar HTTPS for WordPress https://www.saotn.org/ssl-wordpress-move-wordpress-site-https-definitive-guide/ -->
  <!-- see https://www.saotn.org/redirect-http-to-https-on-iis/ and
    https://www.saotn.org/iis-url-rewrite-redirect-http-to-https/ for more 
  information -->
  <rule name="example.com http to https" stopProcessing="true">
    <match url="(.*)" ignoreCase="true" />
    <conditions logicalGrouping="MatchAll">
      <add input="{HTTP_HOST}" pattern="^(www.)?example\.com$" />
      <add input="{HTTPS}" pattern="off" />
      <add input="{URL}" pattern="(.*)" />
    </conditions>
    <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
  </rule>

Your TEST_SITE should be in the {URL} input pattern, captured in {R:1}.

0
votes

Please try this out

<rule name="HTTP To HTTPS" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTPS}" pattern="OFF" />
            <add input="{HTTP_HOST}" pattern="^(([^\.]+)\.)?example.com$" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:0}" />
    </rule>