1

I need to make a global rule that rewrites all http://example.com/ to https://example.com/. I host multiple domains and my attempts either don't work or work on all domains.

I put this in my applicationHost.config:

<globalRules>
  <rule name="Rewrite test" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
      <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" />
  </rule>
</globalRules>

<globalRules>
  <rule name="kommunetv SSL">
    <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="http://(.*\.kommunetv.no)(/?.*)" />
       </conditions>
       <action type="Rewrite" url="https://{C:1}{C:2}" appendQueryString="false" />
  </rule>
</globalRules>

Tried various match patterns and also attempts with {HTTP_HOST} conditions. Hoping for some input :)

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • If you want a global rule, but then only apply that to certain sites, you can add conditions with `{HTTP_HOST}`, you said you already done that but you don't show what you have done and how it behaved. – Peter Hahndorf Feb 06 '17 at 01:24
  • You're right, a bit lazy. I updated my original post. – Thomas Johansen Feb 06 '17 at 07:27

1 Answers1

0

<globalRules> in applicationHost.config apply to the entire server - all domains.

<conditions logicalGrouping="MatchAll">
    <add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true"/>

This only checks for {HTTPS}, so yes, this will redirect all domains.

<conditions>
    <add input="{HTTP_HOST}" pattern="http://(.*\.kommunetv.no)(/?.*)" />
</conditions>
<action type="Rewrite" url="https://{C:1}{C:2}" appendQueryString="false" />

The HTTP_HOST variable contains only the hostname from the request (ie. the value of the HTTP Host header). It does not contain the scheme or URL-path. So, the above condition will never match and the rule will do nothing. If it did match then it would create a redirect loop as you need to also check the HTTPS state.

You need to check both HTTPS and HTTP_HOST. For example, something like:

<conditions logicalGrouping="MatchAll">
    <add input="{HTTPS}" pattern="^off$" />
    <add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" />
MrWhite
  • 12,647
  • 4
  • 29
  • 41