httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Krist van Besien" <krist.vanbes...@gmail.com>
Subject Re: [users@httpd] RewriteMap and RewriteRule
Date Sat, 06 Dec 2008 16:15:37 GMT
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?
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]

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.

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


Mime
View raw message