Search code examples
asp.net-mvcmod-rewriteurl-rewritingisapi-rewrite

How do you use rewritecond with a lookup in a map


I have a map that I use to rewrite urls in my asp.net MVC application. I would like this rule to be conditional on the presence of the lookup value in the map.

RewriteMap contentmap txt:content/maps/contentmap.txt [NC]
RewriteRule home/([^/]*)$ /home/${contentmap:$1}/$1 [NC,L]

this takes the url home/some-content-id and rewrites it as home/someaction/some-content-id

The problem I am running into is sometimes the pattern home/some-other-content-id may not match in the content map. Thats ok but the rule still attempts (or seemingly so) to rewrite when it would be better if it didn't.

My initial thought is to put a rewritecond above the rule, but how do you do the lookup?


Solution

  • Have you tried:

    RewriteMap contentmap txt:content/maps/contentmap.txt [NC]
    RewriteCond ${contentmap:$1}  >""  [NC]
    RewriteRule home/([^/]*)$ /home/${contentmap:$1}/$1 [NC,L]
    

    Meaning if ${contentmap:$1} results in anything larger than an empty string (""), then continue with the rule.