Infinite Redirects

Topics: Developer Forum
Nov 1, 2010 at 8:53 AM

I have an unusual situation for which I need to find a workaround. I just implemented SEF URLs on a classical ASP website and IIRF appears to do the job just fine. Now that everyone is satisfied with the results, I want to instruct Google to replace all references to ".asp" pages with their ".html" versions. Here is a sample set of rules:

RedirectRule ^/properties.asp         /properties/index.html [R=301]
RewriteRule  ^/properties/index\.html /properties.asp        [U,QSA]
In simpler words, I want IIRF to fire "/properties.asp" page if someone requests "/properties/index.html" -- AND -- I want IIRF to send a 301 redirect header when someone directly requests "/properties.asp". The above rules should be sufficient for the job but instead, for both requests i.e. "/properties.asp" and "/properties/index.html", IIRf sends a 301 redirect to "/properties/index.html". This causes the web browser to get stuck in an infinite redirection loop.

Now I believe it is because of the "recursion" built into IIRF but IMO, this should not be the case. According to documentation, RedirectRule has in implicit "[L]" flag which means that if the condition is satisfied, it will be the last rule processed and no recursion will tale place but probably not the case. I tried moving the swapping the rule position but that did not help. Please advice.

Nov 4, 2010 at 1:53 AM

The rule recursion (or iteration) that is performed by IIRF contributes to the problem, but the solution is to correct your rules. 

Here's what's happening.  Imagine someone requests /properties/index.html.  IIRF rewrites it to /properties.asp.  IIRF then iterates on the rules, because there is no [L] flag on the RewriteRule that has just fired.  Ok, now /properties.asp gets REDIRECTED to /properties/index.html.  As you are aware, redirection is an implicit "last rule" action. So IIS responds to the original request (which, you will recall was /properties/index.html) with a 301 response code, and a new URL of /properties/index.html.  Now, if you are particularly astute, you will see that the new location provided with the 301 is the same as the originally requested URL.  If it were YOU requesting this URL, you would pretty quickly detect something wrong, and you'd stop.  But a browser isn't so smart, and it will follow the 301 response, requesting /properties/index.html, which will result in another 301, and so on, and so on.

To fix this, apply the [L] flag to the RewriteRule.  That way, IIRF will rewrite /properties/index.html to /properties.asp, and will not process any further rules.  Which is what you want, I think.

Good luck.


Nov 4, 2010 at 6:48 AM
Edited Nov 4, 2010 at 6:49 AM

Well explained, thanks. I've added the [L] flag to all "RewriteRule" as described and everything is fine.

One minor question is that that I am sure that all of my "RewriteRule" rules (there are ~100) are mutually exclusive and therefore I do not need recursion. I tried to disable it by removing all [L] modifier flags and adding this as the first line:

IterationLimit 1

This did not stop IIRF from iterating the rules one more time. I tried:

IterationLimit 0

And that worked. Just wondering if it is safe to use since it is not documented?




Nov 4, 2010 at 1:27 PM

I'll have to look into that.  If it's not documented, then I didn't think about it, which means... I don't know.  I'll have to look at the code and see what happens in that boundary case.


Nov 4, 2010 at 1:28 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jan 7, 2011 at 1:16 PM

IterationLimit of 0 (zero) is safe to use. 

It's now documented as such, in v2.1.1.25 of IIRF.