Thanks Adam,

I was pretty sure about returning the constant, but I didn't see where the location should go. Looks like err_headers_out works, thanks.

-David Stewart



On Mar 25, 2009, at 12:36 PM, Adam Prime wrote:

David Stewart wrote:
How do you replicate the redirect functionality of mod_rewrite in a mod_perl 2 PerlTransHandler? I'm writing a PerlTransHandler to overcome some limitation of mod_rewrite and want to redirect requests to canonical URLs in some cases (e.g. replicate the functionality of the [R,L] flags in mod_rewrite). What's the best way to do this from the handler?

Here are some obviously simplified, untested examples, i think R is a 302, but i've included the 301 as well.

for a 302:

use Apache2::RequestRec;
use Apache2::Const -compile => qw(REDIRECT);

sub handler {
   my $r = shift
   $r->err_headers_out->add(Location => q[your url here]);
   return Apache2::Const::REDIRECT;
}

1;


for a 301:

use Apache2::RequestRec;
use Apache2::Const -compile => qw(HTTP_MOVED_PERMANENTLY);

sub handler {
   my $r = shift;
   $r->err_headers_out->add(Location => q[your url here]);
   return Apache2::Const::HTTP_MOVED_PERMANENTLY;
}

1;

Adam

Reply via email to