regex - Rewrite maps in IIS7 — how to make the match optionally include a trailing slash? -
i have read top 30 google hits several combinations of iis rewrite map condition
, on, can't find decent documentation, either on microsoft.com site or elsewhere.
i have bunch of rewrite maps in iis7 process irrespective of whether or not followed trailing slash. www.foo.com/bar
, www.foo.com/bar/
should both match rule.
<rewrite> <rewritemaps> <rewritemap name="shorturls"> <add key="/terms" value="/en-us/terms-and-conditions/"/> <add key="/privacy" value="/en-us/privacy-and-cookies/"/> <add key="/buy" value="/en-us/where-to-buy/"/> </rewritemap> </rewritemaps> <rules> <rule name="short url redirects"> <match url="^/?(.+)/?$" /> <conditions> <add input="{shorturls:{request_uri}}" pattern="(.+)"/> </conditions> <action type="redirect" url="{c:1}" appendquerystring="true"/> </rule> </rules> </rewrite>
now works well, except way can find make /terms/
match first key in rewrite map duplicate map, reads:
<rewritemap name="shorturls"> <add key="/terms" value="/en-us/terms-and-conditions/"/> <add key="/privacy" value="/en-us/privacy-and-cookies/"/> <add key="/buy" value="/en-us/where-to-buy/"/> <add key="/terms/" value="/en-us/terms-and-conditions/"/> <add key="/privacy/" value="/en-us/privacy-and-cookies/"/> <add key="/buy/" value="/en-us/where-to-buy/"/> </rewritemap>
this seems ridiculously inelegant, given i'm using regular expressions match them in first place. adding /?
condition input or condition pattern doesn't seem work.
i have seen the answer iis7 rewrite map regex? mentions regular expressions cannot used (quoting using rewrite maps in url rewrite module) but, have commented there, seems relate specific examples being given before text, rather wholesale "this can never work".
what missing? there must means of doing this; missing obvious?
this should it:
<rewrite> <rewritemaps> <rewritemap name="shorturls"> <add key="terms" value="/en-us/terms-and-conditions/"/> <add key="privacy" value="/en-us/privacy-and-cookies/"/> <add key="buy" value="/en-us/where-to-buy/"/> </rewritemap> </rewritemaps> <rules> <rule name="short url redirects"> <match url="^(.+?)/?$" /> <conditions> <add input="{shorturls:{r:1}}" pattern="(.+)" /> </conditions> <action type="redirect" url="{c:1}" appendquerystring="true"/> </rule> </rules> </rewrite>
you quite close; needed make 3 small changes:
- removed leading slashes in keys in rewrite map
- used non-greedy quantifier
+?
in rule's match - used reference match
{r:1}
in condition input
i share experience in having trouble finding decent documentation; had experiment way through, following articles:
Comments
Post a Comment