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

I'm totally new to perl and I'm trying to modify some code that's being moved from a Unix to a Windows box. I am triggering a perl script from within a JSP. The intent is to open a different webpage. I know there are better ways to do that, but I need the capability to run a perl script and depending on the outcome open a webpage.
#!c:\perl\bin\perl.exe use strict; #use CGI::Carp qw(fatalsToBrowser); use CGI; use warnings; my $query= new CGI; #print "HTTP/1.0 200 OK\n"; print "Content-Type: text/html\n\n\n"; print $query->redirect('http://localhost/rbsaws/');
The output I get is "Status: 302 Found Location: http://localhost/rbsaws/ " instead of the webpage I hoped for. I get the same result when running it from the command line. I've tried different variations on the above code found elsewhere with no improvement. I'd appreciate any suggestions you might provide.

Replies are listed 'Best First'.
Re: url redirection
by Corion (Patriarch) on Dec 04, 2010 at 16:32 UTC

    You output malformed headers.

    I suggest outputting what HTTP actually specifies:

    print "HTTP/1.0 302 Somewhere else\r\n"; print "Content-Type: text/html\r\n"; print $query->redirect('http://localhost/rbsaws/'), "\r\n"; print "\r\n"; # signal end of headers

    Update: Fixed wrong HTTP result code, thanks moritz

      yuck!

      For starters, you're ending the header a total of three times! Furthermore, ->redirect already sets the status line, so you're setting the status line twice with two different titles.

      All that's needed is:

      print $query->redirect('http://localhost/rbsaws/');

      The OP seems to want a Content-Type header for some reason. Is he trying to provide an HTML alternative for browsers that don't support 302 (not that there is such a thing)? If so, he could do the following to add the Content-Type header:

      print $query->redirect( -uri => 'http://localhost/rbsaws/', -content => 'text/html', ); print(q{Please click <a href="http://localhost/rbsaws/">here</a>});
        You're right, it works with only the
        print $query->redirect('http://localhost/rbsaws/');
        I believe I had tried this originally, but had other print statements in front of it which apparently causes a problem. Thanks.

      FWIW if you use CGI through Apache there's no need for the HTTP/1.0 part. And if you do a redirect, you shouldn't be printing 200 OK too.

      So far it worked fine for me to print nothing except the CGI->redirect header:

      use CGI; print CGI->redirect('http://example.com/');
        Not sure what I'm missing, but
        #!c:\perl\bin\perl.exe use strict; #use CGI::Carp qw(fatalsToBrowser); use CGI; use warnings; print $CGI->redirect('http://localhost/rbsaws/');
        produces a "CGI Error The specified CGI application misbehaved by not returning a complete set of HTTP headers." for me.
        That took care of it. Thanks for the help.