FredV
FredV

Reputation: 61

Too many redirects rewrite loop

To redirect my visitors to https I used this rule in my web.config:

  <rewrite>
    <rules>
      <clear />
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" /> 
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>

But I get errors (too many redirects) when I try to visit the website, it seems it gets stuck in a loop.

https://example.com and https://www.example.com are ok.

But these addresses result in an error:

Can anyone help me with this?

Upvotes: 0

Views: 6739

Answers (2)

FredV
FredV

Reputation: 61

I found a solution in these rules:

    <rules>
       <clear />
         <rule name="Redirect to HTTPS" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
                <add input="{HTTPS}" pattern="OFF" ignoreCase="true" />
              </conditions>
              <action type="Redirect" url="http://example.com/{R:1}" />
            </rule>
            <rule name="Redirect to WWW" stopProcessing="true">
              <match url=".*" />
              <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^example\.com$" />
              </conditions>
              <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
            </rule>
        </rules>

Upvotes: 0

If You have an endless loop of redirecting. Try this tips.

"login.php" redirects to "login.php" if you're not logged in. "login.php" redirects to "login.php" if you're not logged in. "login.php" redirects to "login.php" if you're not logged in. "login.php" redirects to "login.php" if you're not logged in. etc.

You should probably make the redirect happen only when the current page is not "login.php"; i.e. remove that logic from this page.

Upvotes: 1

Related Questions