We have had an issue with IIS Rewrite 2.0 on one particular rule on our installation that is causing us a particular headache. We are looking for URLs containing multiple consecutive slashes and rewriting them into single slashes for SEO purposes (we do not have control of external llinks containing these multiple consecutive slashes. Normally IIS handles these multiple slashes properly (//resources/ is interpreted as /resources/ by IIS) but we still want them rewriting for SEO purposes.
As an example, //resources/ is being written and redirected to /resources/, ////media-centre//news// is being rewritten to /media-centre/news/, and so on.
This rule in itself is working correctly when typing //resources/ (you are redirected to /resources/), but it causes a knock-on issue. If I then go to /resources/, this will cause an infinite 301 loop.
The rule we are using :-
<rule name="RemoveMultipleSlashes" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{UNENCODED_URL}" pattern="(.*?)[/]{2,}(.*?)" />
<add input="{HTTP_USER_AGENT}" pattern=".*" />
</conditions>
<action type="Redirect" url="{URLRewriteCleaner:{UNENCODED_URL}}" />
</rule>
Notes :-
-We match all URLs so that we can add a condition for the user agent (which should force the rule to skip caching, but seems to be ineffective)
-URLRewriteCleaner is a straight-forward URL Rewrite Provider that rewrites multiple slashes into singular slashes. We also tried this rule with a regular expression but got the same results.
Everything points at the rule being evaluated for /resources/ despite not matching the conditional input. /resources/ will work on a site refresh until someone access the URL at //resources/ (triggering the rule), at which point /resources/ starts the 301 loops.
The issue goes away if we turn off IIS rewrite caching via the registry (we hoped the user agent condition would make the rule avoid the cache).
Does anyone have any thoughts or other suggestions?