Arman Hayots
Arman Hayots

Reputation: 2508

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?

Upvotes: 7

Views: 8906

Answers (2)

David Fawzy
David Fawzy

Reputation: 1076

<rewrite>
    <rules>
            <rule name="Redirect to http" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*" negate="false" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
            </rule>
        </rules>

add that to the system.webserver section

Upvotes: 0

Arman Hayots
Arman Hayots

Reputation: 2508

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.

Upvotes: 5

Related Questions