We need to rewrite redirects coming back from Apache2 mod_proxied sites
(specifically, change http urls to https urls).  We already have
ProxyErrorOverride turned on for our proxied pages (and we need it for
other things), so we decided that using an "ErrorDocument 302" and a
little custom handler would work out best. Getting the "Location" out of
the original redirect is no problem -- we just use:

$r->prev->headers_out->get('Location');

BUT, for the life of me I cannot figure out to rewrite the "Location:"
header to something different!  For testing, I set up a little handler
that should rewrite every redirect to go to http://www.perl.org/.  When I
use this as an ErrorDocument, it runs fine and I see the original
"Location" header print out properly to the error logs, but no new location
header is set (argh!).  The existing Location header is sent through no
matter how many ways I try to reset it.

So, how do I reset the "Location" header from an ErrorDocument 302 handler?
Any ideas?

Here's my test script (running under Apache/2.0.55 and the newest version
of mod_perl):

---
package Apache2::Redirect;
use strict;

# ErrorDocument 302 /redirect
#
# <Location /redirect>
#    SetHandler modperl
#    PerlResponseHandler Apache2::Redirect
# </Location>


use Apache2::RequestRec ();
use Apache2::RequestUtil ();
use APR::Table ();
use Apache2::Const -compile => qw(OK HTTP_OK DECLINED REDIRECT);

sub handler {
    my $r = shift;

    return Apache2::Const::DECLINED if $r->is_initial_req; # only subrequests!
    return Apache2::Const::DECLINED unless my $prev = $r->prev; 

    my $location = $prev->headers_out->get('Location');
    warn 'Location is '.$location;

    # None of these appear to set the headers...
    $r->headers_out->set(Location => 'http://www.perl.org/');
    $r->err_headers_out->set(Location => 'http://www.perl.org/');
    $prev->headers_out->set(Location => 'http://www.perl.org/');
    $prev->err_headers_out->set(Location => 'http://www.perl.org/');

    # Are either of these needed?
    $r->status(Apache2::Const::REDIRECT);
    $prev->status(Apache2::Const::REDIRECT);

    # What should I return?  Maybe this affects header output?
    # return Apache2::Const::REDIRECT;
    return Apache2::Const::OK;
}

1;

---

Thanks!

- Matt

Reply via email to