Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

CGI Redirect with Status

by footpad (Abbot)
on Jun 04, 2003 at 19:51 UTC ( #263134=perlquestion: print w/replies, xml ) Need Help??

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

The apprentice tries to dust off and improve some unused skills...

I'm creating a 404 ErrorDocument handler to perform redirects based on file name changes on my site's source files. Here's an abbreviated version of what's currently working:

#!/usr/bin/perl -wT use strict; use CGI qw(:standard); $| = 1; my $cgi = new CGI; my $dir = '/my/httpd/path'; # spoofed my $old = $ENV{ 'REDIRECT_URL' } || ''; my $new = ''; if ( $old =~ /(.*?)\.foo/i ) { $new = "$"; if ( -e "$dir$new" ) { print redirect( $new ); exit; } } else { doStandard404(); }

My petition is, "Is there a way to set the status code of the redirection?" (The idea, as you might expect, is to return a 301 indicating that the document has moved permanently, so the search engines will notice the change and update their databases accordingly) Alternatively, does already return an appropriate error code?

I've checked the Friendly Docs, but didn't see anything. Also, a few google searches didn't turn up anything that seemed relevant, though may be due to my lack of tilly-fu.)

Details, if they're relevant, include Apache 2.0, Perl 5.6.1, 2.93

Thanks in advance...


Replies are listed 'Best First'.
Re: CGI Redirect with Status
by Ovid (Cardinal) on Jun 04, 2003 at 20:40 UTC

    I've never been quite sure why this is, but hard-codes the redirection value:

    sub redirect { my($self,@p) = self_or_default(@_); my($url,$target,$cookie,$nph,@other) = rearrange([[LOCATION,URI,UR +L],TARGET,['COOKIE','COOKIES'],NPH],@p); $url ||= $self->self_url; my(@o); foreach (@other) { tr/\"//d; push(@o,split("=",$_,2)); } unshift(@o, '-Status' => '302 Moved', '-Location'=> $url, '-nph' => $nph); unshift(@o,'-Target'=>$target) if $target; unshift(@o,'-Type'=>''); my @unescaped; unshift(@unescaped,'-Cookie'=>$cookie) if $cookie; return $self->header((map {$self->unescapeHTML($_)} @o),@unescaped +); }

    There are various ways to deal with this, none of them completely satisfactory. Perhaps the easiest would be to generate your own redirect header rather than rely on


    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: CGI Redirect with Status
by sutch (Curate) on Jun 04, 2003 at 20:41 UTC
    Try it!


    #!/usr/bin/perl -Tw use CGI qw(:standard); my $cgi = new CGI; print redirect('');
    Results in:
    bash-2.05$ ./ Status: 302 Moved Location: bash-2.05$

      Your results show his problem. He wants a 301 status code (moved permanently), not a 302 status code (Found). See status code definitions for more information.


      New address of my CGI Course.
      Silence is Evil (feel free to copy and distribute widely - note copyright text)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://263134]
Approved by dorko
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2023-03-28 21:48 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (69 votes). Check out past polls.