<?xml version="1.0" encoding="windows-1252"?>
<node id="704425" title="Failure to redirect when my CGI::Application powered site is used under mod_perl2" created="2008-08-14 16:42:50" updated="2008-08-14 12:42:50">
<type id="115">
perlquestion</type>
<author id="194370">
skx</author>
<data>
<field name="doctext">
&lt;p&gt;I've got an issue with redirection failing from my CGI::Application when running under mod_perl.&lt;/p&gt;
&lt;p&gt;I'm very new to mod_perl so I could well believe that I've done something wrong.  My configuration for Apache2 + mod_perl is very minimal:&lt;/p&gt;
&lt;code&gt;
ServerName ...

Alias /cgi-bin/ /mf/cgi-bin/
&lt;Location /cgi-bin&gt;
        SetHandler      perl-script
        PerlHandler     ModPerl::Registry
        PerlSendHeader  On
        Options         +ExecCGI
&lt;/Location&gt;
&lt;/code&gt;
&lt;p&gt;This works for most of my application, and appears to improve response time significantly - however there is one problem.  My redirections, as coded in the CGI::Application do not work.&lt;/P&gt;
&lt;p&gt;I've pared this down to a minimal clone of my code - I have a base class implementing a redirect method, and I have a subclass which uses that to redirect a few times.&lt;/p&gt;
&lt;p&gt;In plain CGI-mode this works.  In mod_perl it gives me the following output direct to my browser:&lt;/p&gt;
&lt;pre&gt;
redirect&amp;lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&amp;gt;
&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;200 OK&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;&amp;lt;body&amp;gt;
&amp;lt;h1&amp;gt;OK&amp;lt;/h1&amp;gt;
&amp;lt;p&amp;gt;The document has moved 
&amp;lt;a href="http://example.com/"&amp;gt;here&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;hr&amp;gt;
&amp;lt;address&amp;gt;Apache/2.2.9 (Debian) PHP/5.2.6-2+b1 with
 Suhosin-Patch mod_ssl/2.2.9
 OpenSSL/0.9.8g mod_musicindex/1.2.2
 mod_perl/2.0.4 Perl/v5.10.0
 Server at mail-scanning.com Port 80&amp;lt;/address&amp;gt;
&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;

&lt;/pre&gt;
&lt;p&gt;Now here is my example code which exhibits the same problem:&lt;/p&gt;
&lt;code&gt;
#!/usr/bin/perl -w

use strict;
use warnings;
use CGI::Application;

#
#  Base package
#
package Base;
use base 'CGI::Application';

sub redirectPage
{
    my ( $self, $url ) = (@_);
    $self-&gt;header_add( -location =&gt; $url );
    $self-&gt;header_type('redirect');
}

#
#  Derived package
#
package Derived;
use base 'Base';


sub setup
{
    my( $self ) = ( @_ );
    $self-&gt;run_modes( homepage =&gt; 'homepage' );

    $self-&gt;start_mode('homepage');
    $self-&gt;mode_param('mode');
}

#
#  Dummy method to redirect to an external site.
#
sub homepage
{
    my( $self ) = ( @_ );
    return ( $self-&gt;redirectPage("http://example.com/") );

}


#
#  Instance script
#
package main;

my $derived = new Derived();
$derived-&gt;run();
&lt;/code&gt;

&lt;p&gt;Any clues, or tips are most welcome.  Any more details I can provide just ask - this is all running upon libapache2-mod-perl2, as included in Debian.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; - Strangly using &lt;code&gt;CGI::Application::Plugin::Redirect&lt;/code&gt; works correctly if I change my code:&lt;/p&gt;
&lt;code&gt;

package Base;
use base 'CGI::Application';
use CGI::Application::Plugin::Redirect;

sub redirectPage
{
    my ( $self, $url ) = (@_);

    return( $self-&gt;redirect( $url ) );
}

&lt;/code&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-194370"&gt;
&lt;a href="http://www.steve.org.uk/"&gt;Steve&lt;/a&gt;&lt;br/&gt;
-- &lt;br/&gt;

&lt;/div&gt;&lt;/div&gt;
</field>
</data>
</node>
