You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Jeff Fulmer <je...@joedog.org> on 2007/09/21 19:06:00 UTC

URI redirection

Hello list!

I have a functional requirement that is currently delivered with
mod_rewrite maps but it would be much more maintainable with something
that offered programmatic logic. I started to look at mod_perl and I
suspect it might be perfect. I'm using mod_perl version 1.9.x

I was hoping someone can push me in the right direction. At this point I
know enough to be dangerous ;-)

I'll have a function that takes a URI as an argument. The function will
look for that URI in a configure file(s). If it finds a match, it will
return a URI  to where the client should be redirected. If it doesn't find
a match, then the client should go to the URI they requested. Let's call
the function uri_map in the pseudo code below:

$redirect = uri_map($r->uri);
if(defined($redirect)){
  $r->header_out(Location => '/env.jsp');
  return REDIRECT;
} else {
  # and I'm stuck here;
  # allow the request to pass thru to the server
  # so that the client goes to the requested URI.
}

I'd like to invoke this code for *every* request made to the server. To
meet that requirement, it seems like <Location /> is a good place to
reference the handler.

Any insight would be appreciated.

Cheers,
Jeff


Re: URI redirection

Posted by Michael Peters <mp...@plusthree.com>.
Jeff Fulmer wrote:

> I have a functional requirement that is currently delivered with
> mod_rewrite maps but it would be much more maintainable with something
> that offered programmatic logic. I started to look at mod_perl and I
> suspect it might be perfect. I'm using mod_perl version 1.9.x

Well, first piece of advice, ditch that version and upgrade to the latest 2.0.
1.9.x was the 2.x alpha series. It has lots of bugs and depending on which
version you have may be a completely different API.

> I'll have a function that takes a URI as an argument. The function will
> look for that URI in a configure file(s). 

How many URLs are we talking? Depending on how many you have and how much memory
you have it would make sense to read all those urls when apache is started and
put them into a Perl hash. This will make looking them up really fast.

> If it finds a match, it will
> return a URI  to where the client should be redirected. If it doesn't find
> a match, then the client should go to the URI they requested. Let's call
> the function uri_map in the pseudo code below:
> 
> $redirect = uri_map($r->uri);
> if(defined($redirect)){
>   $r->header_out(Location => '/env.jsp');
>   return REDIRECT;
> } else {
>   # and I'm stuck here;
>   # allow the request to pass thru to the server
>   # so that the client goes to the requested URI.

You probably want to run your code as a TransHandler (using the PerlTransHandler
directive. If you did so, you'd just let Apache know what the new url should be
and then tell it your done. See this:

http://perl.apache.org/start/tips/favicon.html

> I'd like to invoke this code for *every* request made to the server. To
> meet that requirement, it seems like <Location /> is a good place to
> reference the handler.

That seems like a good spot. And if you plan on using it for ever request, it's
a good reason to read the url map into memory before you begin.

-- 
Michael Peters
Developer
Plus Three, LP