httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Clodoaldo Pinto Neto" <clodoaldo.pi...@gmail.com>
Subject Re: [users@httpd] RewriteMap and RewriteRule
Date Sat, 06 Dec 2008 17:51:02 GMT
2008/12/6 Clodoaldo Pinto Neto <clodoaldo.pinto@gmail.com>:
> 2008/12/6 Krist van Besien <krist.vanbesien@gmail.com>:
>> On Sat, Dec 6, 2008 at 3:05 AM, Clodoaldo Pinto Neto
>> <clodoaldo.pinto@gmail.com> wrote:
>>> I'm using this config in Apache 2.2.3/Centos 5:
>>>
>>> RewriteMap redirect txt:/var/www/html/example/redirect.txt
>>>
>>> RewriteRule ^/(a|b|c|d|e)$ http://example.com/${redirect:$1} NC,R=permanent,L]
>>>
>>> redirect.txt:
>>> a x
>>> b y
>>> c z
>>> d k
>>> e j
>>>
>>> Since the redirected pages are many i would like to not have to put
>>> them all in RewriteRule ^(a|b|c|d|e)$ using in instead the
>>> redirect.txt map to populate that rewrite rule. Is it possible? If not
>>> what else can be done?
>>
>> Basically you want the rule not to change the URL if there is no macht
>> in redirect.txt, right?
>
> Exactly
>
>> I had a similar need a while ago. I can't look at the exact solution I
>> implemented then (as I'm not in the office at the moment) but it was
>> something like this:
>>
>> RewriteCond ${redirect:$1}      (^.+$)
>> RewriteRule  (.*)                       http://example.com/%1
>> [NC,R=permanent,L]
>
> Almost that. It worked like this:
>
>      RewriteMap redirect txt:/var/www/html/example/redirect.txt
>      RewriteCond ${redirect:$1} (^.+$)
>      RewriteRule /(.+)$ http://example.com/%1 [NC,R=permanent,L]

A bit more elaborate. Match any case:

      RewriteMap redirect dbm=db:/var/www/html/example/redirect.db
      RewriteMap tolower int:tolower
      RewriteCond ${tolower:$1} (^.+$)
      RewriteCond ${redirect:%1} (^.+$)
      RewriteRule /(.+)$ http://example.com/%1 [NC,R=permanent,L]

>
>>
>> This gets processed in the following order:
>> - (.*) is matched against the URL. URL is captured in $1
>> - $1 gets looked up in the redirect map. The result gets matched
>> against an non empty line (that's what ^.+$ is) and if there is a
>> match, it getst captured in %1, and the RewriteRule can proceed.
>> - The rule gets rewriten to http://example.com/%1 (%1 is what we
>> captured in the RewriteCond).
>>
>> The advantage of this construct is that if URL isn't found in your
>> rewritemap than the whole RewriteRule fails, and rule processing
>> continues, which makes it possible to have other rules for the cases
>> you can't solve with your rewritemap.
>> A last note: If your redirect map gets large use dbm maps, not text
>> maps. They are a lot faster.
>
> This explanation was what i needed. Without it  i would spend the
> whole weekend trying.
>
> Thanks, Clodoaldo
>
>>
>> Krist
>>
>> --
>> krist.vanbesien@gmail.com
>> krist@vanbesien.org
>> Bremgarten b. Bern, Switzerland
>> --
>> A: It reverses the normal flow of conversation.
>> Q: What's wrong with top-posting?
>> A: Top-posting.
>> Q: What's the biggest scourge on plain text email discussions?
>>
>> ---------------------------------------------------------------------
>> The official User-To-User support forum of the Apache HTTP Server Project.
>> See <URL:http://httpd.apache.org/userslist.html> for more info.
>> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>>   "   from the digest: users-digest-unsubscribe@httpd.apache.org
>> For additional commands, e-mail: users-help@httpd.apache.org
>>
>>
>

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Mime
View raw message