RewriteMap for RedirectRule without redirect if value is not found

Topics: Developer Forum, User Forum
Jun 3, 2010 at 10:15 PM


I have used the ISAPI filter to create friendly URLs for my left navigation. For example: into something like I have used RewriteMap and created the text file with all conversions and it works great.

The problem: I need to write a RedirectRule for the old URL to display the friendly URL as showing it as a 301 Redirect; this is required in order to keep/link the ranking between the 2 version URL. So I did create a reverse mapping and use the RedirectRule, but the problem is that I need to apply the redirect only to those links that I have converted so basically if the map does not include the value searched - I would like not to apply the redirect. I have tried to use the default option and $1 after the | , but that does not seem to work. 


RewriteMap  namerev txt:nameReverse.txt 

RedirectRule ^/main.asp(.*)   /show-${namerev:$1|$1}  [R=301]

So when I put the rule above shows which is fine, but when I use I get something strange : which translates to the first 2 characters of the $1. But eeven if I get the full $1 (?request=search&name=OTHERNAME) I would like to show it without the show- prefix.

This issue is raised by the fact that only some of my links are using the friendly URLs and the rewrite rule while others do not. So for those that do not - I would like the RedirectRule to let the request unchanged.



Jun 3, 2010 at 11:52 PM

Last week a user reported a problem in the RewriteMap.  The symptom was that substitutions that relied on the default value of RewriteMap were being truncated.  I'm not sure if that's what you're seeing or not.  I fixed this problem in the latest v2.1 release - which is now at  If you don't have that version, get it, and test again.


Jun 4, 2010 at 4:27 PM

Thanks, but I still do not have a solution. Let me try restate what I want: I would like to use a redirect rule only and only if the link is to be found in the mapping list - if it is not found I do not want to even go to that redirect rule.

The issue is that our links are generic and I can't use a pattern so this is why I used ^/main.asp(.*)   whic basically covers all our links - hoever I want the redirect to applyonly when the link is found in the map list.

How to do that?


Jun 4, 2010 at 8:59 PM
Edited Jun 4, 2010 at 9:01 PM

Thanks for restating what you want very clearly.  All the details about your navigtion approach didn't help clarify things for me at all.
To do what you want, apply a RewriteCond to the RedirectRule. The condition you test should be the result of the map substitution.  like this:

RewriteCond  ${mapname:key|NOTFOUND}  !^NOTFOUND$

In English, it says "if the result of the map substitution is NOT  NOTFOUND".   The condition will be true when the key is found in the map. Of course the key can be a reference to a capture from the URL ($1, $2, etc) or a reference to a capture from the prior condition, or a constant, or a server variable, etc.



Jun 7, 2010 at 7:31 PM

Thanks. I did use the RewiteCond and it seems fine except that the search in the map does not seem to be working when the link has a space in the middle.

My rules are:

RewriteMap  categoryrev   txt:e:\inetpub\winestore\winechateau\categoryReverse.txt
RewriteCond  ${categoryrev:$1|NOTFOUND}  !^NOTFOUND$
RedirectRule ^/main.asp(.*)   ${categoryrev:$1|/main.asp$1}  [R=301]

I am attaching the lines from the map I have. For some reason the last 2 using Cabernet Sauvignon does not seem to be getting the result.

?request=search&country=argentina&type=w&nav=30                                                                                                                 /category_Argentina-Wine                         
?request=search&variety=Cabernet%20Sauvignon&country=argentina&type=w&nav=30                                                                                    /category_Argentina-Cabernet-Sauvignon           
?request=search&variety=Cabernet Sauvignon&country=argentina&type=w&nav=30                                                                                      /category_Argentina-Cabernet-Sauvignon           


Here is what i see in the logs:

Mon Jun 07 14:25:07 -  2112 - ReplaceServerVariables: alloc 27 bytes
Mon Jun 07 14:25:07 -  2112 - ReplaceServerVariables: in='${categoryrev:$1|NOTFOUND}' out='${categoryrev:$1|NOTFOUND}'
Mon Jun 07 14:25:07 -  2112 - EvalCondition: ts1 '${categoryrev:$1|NOTFOUND}'
Mon Jun 07 14:25:07 -  2112 - GenerateReplacementString: map?
Mon Jun 07 14:25:07 -  2112 - GenerateReplacementString: alloc 82 bytes
Mon Jun 07 14:25:07 -  2112 - GenerateReplacementString: result '?request=search&variety=Cabernet Sauvignon&country=argentina&type=w&nav=30'
Mon Jun 07 14:25:07 -  2112 - MapKey: map(categoryrev) key(?request=search&variety=Cabernet Sauvignon&country=argentina&type=w&nav=30)
Mon Jun 07 14:25:07 -  2112 - MapKey: map(categoryrev) key(?request=search&variety=Cabernet Sauvignon&country=argentina&type=w&nav=30) no value found

Jun 9, 2010 at 6:40 PM

Please help. I am not able to locate strings that contains a space (or %20) using RewriteMap. See last message.


Jun 16, 2010 at 10:20 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jun 16, 2010 at 10:25 PM

You could try setting UrlDecoding OFF.  In that case you may be able to use the key value with a %20 in it.

The IIRF documentation describes the map file format and says that keys and values in the map are separated by whitespace.  Therefore it is not possible to specify a key with a literal space in it. By disabling the UrlDecoding, your URL should retain the %20 in place of the space, which should allow you to do the map lookup the way you want.

I haven't tried this but it "should work".