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

Hi,

Reporting after some hours lost with this. I'm trying to just write a simple script that will redirect someone off a page to another one using a Moved Permanently header. I'm using Firefox as a browser and can't seem to get this to work.

#!/usr/bin/perl -w use CGI qw/:standard/; my $q = new CGI; print $q->redirect( -status=>301, -URL=>'http://192.168.1.3' );

If I hit the address of this in the browser sometimes I get a blank page and sometimes I get

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>500 Internal Server Error</title> </head><body> <h1>Internal Server Error</h1> <p>The server encountered an internal error or misconfiguration and was unable to complete your request.</p> <p>Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.</p> <p>More information about this error may be available in the server error log.</p> <hr> <address>Apache/2.2.3 (Debian) DAV/2 SVN/1.4.2 mod_fastcgi/2.4.2 PHP/5 +.2.0-8+etch11 Server at 192.168.1.3 Port 80</address> </body></html>

Also,the script compiles fine. I don't really understand why it's not working , does anyone know ?

Thank you

Replies are listed 'Best First'.
Re: CGI problems
by Your Mother (Archbishop) on Dec 17, 2008 at 04:30 UTC

    Sometimes permissions can bite CGI execution. The folder it's in should probably be 755. The script too. You can shorten, and ever so slightly increase the speed of, the code this way-

    use CGI (); print CGI::redirect( -status => 301, -url => "http://192.168.1.3" );

    When you execute the file/script from the command line, it works, right?

    moo@cow[167]~/bin>perl pm-730807-demo-code Status: 301 Location: http://192.168.1.3

    And while this is a fun exercise to play with, an easier and faster solution is to put this in the relevant .htaccess file.

    redirect 301 /old http://192.168.1.3/new
Re: CGI problems
by almut (Canon) on Dec 17, 2008 at 02:48 UTC

    Anything related in the Apache error log?

    (In case you want to avoid having to look in the error log, you could use CGI::Carp qw(fatalsToBrowser); which would send any fatal error messages to the browser.)