1

I have a very simple URL Rewrite redirect rule that results in an infinite loop. When it fails a "Can't display page" error is displayed. Any error trapping tips or script modifications would be appreciated.

 <rewrite>
    <rules>
        <rule name="Test redirect" enabled="false" stopProcessing="true">
            <match url=".*" />
        <action type="Redirect" url="/pub/" appendQueryString="false" logRewrittenUrl="true" redirectType="Permanent" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^mydomain.*(com|net)$" />
            </conditions>
        </rule>
    </rules>
</rewrite>
  • The rule is located in the wwwroot folder web.config file.
  • The rule redirects to an MVC application in the wwwroot/pub/ folder.
  • The IIS log is showing that the /pub/ folder is being called multiple times in the loop until the request is terminated by IIS.
  • If the rule is disabled and I enter the URL directly it works fine.
  • The same error is seen on different browsers.
  • It is not a browser cache issue.

Mike G
  • 406
  • 1
  • 6
  • 19

3 Answers3

1

The problem turned out to be related to SSL. An HTTPS redirect requires an {HTTPS} condition, as shown below. Without that condition the rule will loop when you attempt to redirect to an https:\ URL. You also must include the entire https:\ URL in the action as shown.

<rule name="Test redirect" enabled="true" stopProcessing="true">
    <match url=".*" />
    <action type="Redirect" url="https://yourdomain.com/pub/" appendQueryString="false" logRewrittenUrl="true" redirectType="Temporary" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^yourdomain.*(com|net)$" />
        <add input="{HTTPS}" pattern="off" />
    </conditions>
</rule>
Mike G
  • 406
  • 1
  • 6
  • 19
0

It seems like your URL Redirect rule is being hit multiple times, since it's matching on all URLs via the <match url=".*" /> directive. This post says that the stopProcessing='true' directive doesn't necessarily work for Redirect rules.

Taking a look at a similar question, you might want to add a block rule first in order to prevent any URLs with the fragment mydomain.com/pub from triggering any rewrite rules and entering your infinite loop scenario:

<rule name="block" stopProcessing="true">
    <match url="^mydomain.*(com|net)/pub.*" />
    <action type="None" />
</rule>
Adil B
  • 14,635
  • 11
  • 60
  • 78
  • Thanks Adil, but it's still looping. I can do a redirect using rules with a web.config file located in the web application folder. But it seems to fail when the rule is applied to the web.config in the wwwroot folder. Any Ideas? – Mike G Dec 28 '17 at 18:27
0

can you review your code and all paths should be absolutes, for example... the scripts

js/jquery.js is a bad path. Correct path is /js/jquery.js

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65