How to run one rewrite rule only once?

Topics: Developer Forum, User Forum
Jul 21, 2008 at 10:01 AM
Hi guys.

I am wondering if it is possible to run a rule only once. I have multiple rules, and some of them (those who replace characters for example) need to be recursive. Therefore I can't just set IterationLimit to 1. But I have one rule that must not be recursive, it's the following:

RewriteRule ^(?:/*(?:1/([^/]+))*)(?:/*(?:2/([^/]+))*)(?:/*(?:3/([^/]+))*)(/*.*)$ what=$1&where=$2&id=$3$4 [I]

This would be used to rewrite this:
http://www.myserver.com/1/john+doe/2/new+york/3/JK+1Rnx2QQ5Vviy9tqlepQ%3d%3d/white/53853494

into this: 
http://www.myserver.com/what=john+doe&where=new+york&id=JK+1Rnx2QQ5Vviy9tqlepQ%3d%3d/white/53853494

But, if this is recursive, it will be hit again and again, because of the last numbered group (/*.*). And the result will be:
Incoming 1: http://www.myserver.com/1/john+doe/2/new+york/3/JK+1Rnx2QQ5Vviy9tqlepQ%3d%3d/white/53853494
Output/incoming 2: http://www.myserver.com/what=john+doe&where=new+york&id=JK+1Rnx2QQ5Vviy9tqlepQ%3d%3d/white/53853494
Output/incoming 3: http://www.myserver.com/what=&where=&id=what=john+doe&where=new+york&id=JK+1Rnx2QQ5Vviy9tqlepQ%3d%3d/white/53853494
Output/incoming 4: http://www.myserver.com/what=&where=&id=what=&where=&id=what=john+doe&where=new+york&id=JK+1Rnx2QQ5Vviy9tqlepQ%3d%3d/white/53853494
...and so on until IterationLimit is reached.

This is no good of course. Any ideas on how to solve this? I'm open for all suggestions.

Thanks in advance.
Coordinator
Jul 22, 2008 at 1:07 AM
Edited Jul 22, 2008 at 1:08 AM

how about a negative lookahead in the regex?  eg, "Only rewrite if the what=xxx is NOT already set. "

Your negative lookahead might be like this:  (?!what=[^&]+)  , which you will need to embed at the proper place in the regex.

There are example negative lookaheads here: 
http://www.codeplex.com/IIRF/Wiki/View.aspx?title=Negative%20Lookaheads&referringTitle=Examples

You can test out variations of regex's with the testdriver.exe tool.