mrc has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use a perl filter to redirect user if some conditions are met.
This is the base filter that should redirect everything:
package My::Filter; use strict; use warnings; use CGI::Cookie (); use Apache2::RequestRec (); use APR::Table (); use Apache2::Const -compile => qw(REDIRECT); my $location = "http://www.test.com"; sub handler { my $r = shift; my $cookie = CGI::Cookie->new(-name=> 'mod_perl', -value => 'awesome'); $r->err_headers_out->add('Set-Cookie' => $cookie); $r->headers_out->set(Location => $location); return Apache2::Const::REDIRECT; } 1;
I'm calling it with
<Directory "/path/to/test/account/"> PerlOutputFilterHandler My::Filter </Directory>
No errors, nothing :(
When I visit a page under that test account headers are not even set, I get empty HTTP response.
What I'm doing wrong? Please advice.

Replies are listed 'Best First'.
Re: perl filter for redirection
by Anonymous Monk on Mar 24, 2010 at 17:50 UTC
    The documentation shows using PerlOutputFilterHandler with Location and Files tags, not Directory. I suggest you try one of those.
      PerlOutputFilterHandler working with <directory> as well. Tested with similar filter.
      I have no idea why this won't work
      $r->headers_out->set(Location => $location);
      Headers are not set
        Problem seems related to
        Apache2::Const::REDIRECT
        If I replace it to
        Apache2::Const::OK
        I can set "Location", but redirection won't really work because response is 200 not 302. The same problem with
        Apache2::Const::HTTP_TEMPORARY_REDIRECT
        Nothing working except Apache2::Const::OK :(
        Any input will be greatly appreciated.