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

Hi, I have been searching for a solution to redirect(both internal and external). But haven't found anything so far. Can anybody please help me ? Below is the code that I tried, but it did not work.
$r->headers_out->set('Location' => $url); $r->status(REDIRECT);
Thanks

Replies are listed 'Best First'.
Re: External and Internal Redirecting in Mod Perl 2
by Anonymous Monk on Nov 15, 2010 at 07:28 UTC
      The code below worked for me for Mod Perl 2.
      use Apache2::Const -compile => qw(OK DECLINED REDIRECT); my $r = shift; my $url = 'http://www.google.com'; $r->headers_out->set(Location => $url); $r->status(Apache2::Const::REDIRECT);
      But something strange happens often. when I call the link in a browser it displays a blank page. But if I refresh a couple of times, it goes to google.com. As the below output of 'Live HTTP headers' indicates I get 200 OK page instead of a 302 page. FYI, I have setup local DNS resolution for modperllab.com and I use ModPerl::Registry as the Response Handler.
      http://www.modperllab.com/migration/redirect_tests.pl GET /migration/redirect_tests.pl HTTP/1.1 Host: www.modperllab.com User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.18) Gec +ko/2010021718 CentOS/3.0.18-1.el5.centos Firefox/3.0.18 GTB7.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0. +8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive HTTP/1.1 200 OK Date: Mon, 15 Nov 2010 09:35:17 GMT Server: Apache/2.2.8 (CentOS) Location: http://www.gmail.com Content-Length: 0 Connection: close Content-Type: text/plain; charset=UTF-8
      Any ideas ? Thanks. Mohammed Shakir
      Looks like
      $r->send_http_header; return OK;
      is missing.
        I think you are right if it is Mod Perl 1. But for Mod Perl 2 there is no method, 'send_http_header'. Any other working solutions for Mod Perl 2 ?
Re: External and Internal Redirecting in Mod Perl 2
by shak380 (Novice) on Nov 16, 2010 at 03:23 UTC
    Hi Does anybody have any idea on this............? Thanks in advance. Mohammed Shakir
      I have fixed this. If anybody has the same issue, I think they can use the way I did. It is given below :
      use Apache2::Const -compile => qw(REDIRECT); $r->headers_out->set('Location' => $url); $r->status(Apache2::Const::REDIRECT);
      Let me know if anybody has any more issues related to this.