7

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0
<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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.