Computer
Computer

Reputation: 2227

Too many redirects with URL Rewrite

I have the below config added for a redirect which works for this domain but for www version it doesnt redirect to the https version of the site

    <rule name="Site Http" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="test.example.uk" />   
      </conditions>
      <action type="Redirect" url="https://www.example.com/{R:0}" />
    </rule>
    <rule name="Site to HTTPS" stopProcessing="true">
        <match url="(.*)" />
        <conditions trackAllCaptures="true">
            <add input="{HTTP_HOST}" pattern="test.example.uk" />
            <add input="{HTTP_HOST}" pattern="www.example.com" />
            <add input="{HTTP_HOST}" pattern="^((www\.)?example.com)$" />
            <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://{C:1}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent"/>
    </rule>

So no matter which domain i have listed i want it to go to the https version of the site (https://www.example.com) but if i enter domain.com it redirects to https, if i enter test.domain.uk it goes to https but if i enter www.example.com it remains on http?

If i add

<add input="{HTTP_HOST}" pattern="www.example.com" />

to the Site Http rule i get too many redirects error in a browser.

How could i redirect www.example.com to https?

Upvotes: 0

Views: 1285

Answers (1)

Ding Peng
Ding Peng

Reputation: 3964

You can try to use this URL Rewrite rule:

<rewrite>
  <rules>
    <rule name="Test" stopProcessing="true">
        <match url="^(.*)$" ignoreCase="false" />
        <conditions>
            <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" />
         </conditions>
      <action type="Redirect" url="https://{SERVER_NAME}{URL}" redirectType="Found" />
    </rule>
  </rules>
</rewrite>

The meaning of the following rule is that if the HTTP header X-Forwarded-Proto contains "https", the rule will not perform redirection.

<add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" />

This rule will avoid circular redirects.

Upvotes: 3

Related Questions