Value not found in map

Topics: Developer Forum
May 30, 2010 at 4:38 AM

I've seen in the redirect example using a map that if a map value is not found that you can redirect to an entirely different URL using

I've got a situation where I'm using a map in RewriteRule, and I'd like to be able to _redirect_ if the map value is not found - is that possible? Is it possible to do it & also not specify the domain?

Somewhat related, I've found that when a rule does not match, and I just stick the value I tried to map onto the end of the querystring, it only appends the first two characters from that value - I found that kind of weird, and it makes it a little hard to debug.  Any thoughts on that one?

This is one of my current rules as an example of what I'm doing:

RewriteRule ^/stuff/([-a-z0-9]*)/$ /stuff.aspx?fid=${stuff:#L$1#E|$1} [I,L]


May 30, 2010 at 4:41 AM

As an illustration for the second problem, if this URL came in using the rule above, and I did not have a map entry for 'things':


I'd see this in my logs:


May 30, 2010 at 12:04 PM

It sounds to me that you want to rewrite if the value is found in the map, and redirect if the value is not found in the map. Is that right?

If that's what you want, then you'll need 2 rules.  One to rewrite, and one to redirect.

You should attach a RewriteCond to the RewriteRule, and use the map-substitution syntax in it.  like this:

RewriteCond  ${stuff:#L$1#E|NOTFOUND}    !^NOTFOUND$
RewriteRule  ^/stuff/([-a-z0-9]*)/$      /stuff.aspx?fid=${stuff:#L$1#E} [I,L]

That rule will fire only if the value is found in the map.  Then you need another rule to redirect.  You can apply the converse condition:

RewriteCond   ${stuff:#L$1#E|NOTFOUND}    ^NOTFOUND$
RedirectRule  ^/stuff/([-a-z0-9]*)/$      /  [I]

About the second problem you observed - that is a surprise. I haven't heard of that before. I'll have to look into that.

May 30, 2010 at 12:16 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
May 30, 2010 at 3:05 PM

The second problem was a bug, which I found and fixed.  I'll post the v2.1.1.22 release soon, with a fix for that.


May 30, 2010 at 4:57 PM

ok, v2.1.1.22 is available on the downloads page. It fixes the bug you reported.


May 31, 2010 at 1:15 PM

Thanks - I just now saw that example in the documentation as I was researching this variation I've got that isn't working.

RewriteMap	vanity				txt:/map_vanity.txt
RewriteCond	%{HTTP_HOST}			^(www\.)?(.*)$ [I]
RewriteCond	${vanity:#L*2#E|NOTFOUND}	!^NOTFOUND$
RewriteRule	^/$				/stuff.aspx?fid=${vanity:#L*2#E} [I,L]

The last RewriteRule no longer has the '*2' variable, so it matches nothing, and I just get: /stuff.aspx?fid=${vanity:}

Any ideas? I've tried a couple variations with no luck.

Thanks so much for any help you could give - I really do like this product quite a bit.

May 31, 2010 at 1:37 PM

Seems like you want *2 to refer to the http host. If that's the case, then why not simplify it:

RewriteMap	vanity				txt:/map_vanity.txt
RewriteCond	${vanity:#L%{HTTP_HOST}#E|NOTFOUND}	!^NOTFOUND$
RewriteRule	^/$				/stuff.aspx?fid=${vanity:#L%{HTTP_HOST}#E} [L]

In the map file you would need to have entries for as well as