简体   繁体   中英

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?

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.

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM