Search code examples
iishttpsurl-rewritingurl-routingurl-rewrite-module

IIS URL-Rewrite: HTTP to HTTPS


I have some site example.org, on which I keep subsites like example.com/project1 and example.com/project2 and so on. I need simple HTTP→HTTPS redirect on some of subsites only, but don't want to write it in codefiles manually.

So I found URL-Rewrite2 module and rule for him:

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

It works at most with one problem: it redirects from http://example.com/project1 to https://example.com/, so it lost's subsite URL part and any args.

How this can be fixed?

UPD: used this rule

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

Subsite redirects normally except that arguments is duplicating. http://example.com/project1?page=2 turns into https://example.com/project1?page=2&page=2. What I'm doing wrong?


Solution

  • Done using this rule:

    <system.webServer>
      <rewrite>
                <rules>
                    <rule name="Redirect to HTTPS" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTPS}" pattern="^OFF$" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}{UNENCODED_URL}" appendQueryString="false" />
                    </rule>
                </rules>
            </rewrite>
      </system.webServer>
    

    Works good on subsites.