I am trying to write a URL rewriter rule in IIS7.
My current reg expression is ^(policy|pricing|contact|gallery)/(.*)
My Rewrite rule is: /{R:1}.aspx?cat={R:2}
policy/ (Keep Slash in this case, WORKS)
gallery/soccer (No slash provided so this WORKS)
gallery/soccer/ (needs to remove last slash)
gallery/soccer/girls/ (needs to remove last slash)
Any ideas would be great, I know how I would approach this in languages like .Net, but I need to strictly do this as a regular expression rule in IIS.
I think the following should work:
The
/?at the end means “match a/one or zero times”, or in other words it is optional. Just adding this to the end wouldn’t work because a/would still be consumed by the.*, so we need to change the.*to.*?so that it is no longer greedy.The
$anchor is necessary so that the match doesn’t end too early.Note that the trailing
/will still be a part of the match, but it will not be a part of the second capture group so your rewrite rule should work properly.See it working: http://www.rubular.com/r/s8IqIlaqoz