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

Hi All,

I was just checking the cgi redirect and wondering that it's not redirecting to the page specified. Below is the simple code:

#!C:/perl/bin/perl.exe use strict; use CGI qw(:standard); print header,start_html,h1('redirecting to a different page'); sleep 5; print redirect('http://localhost/html/test.html'); print end_html;

Upon executing on browser I am getting the output "'redirecting to a different page" but it's not redirecting to the html page mentioned. Am I doing anything wrong? Please let me know.

Thanks.

Replies are listed 'Best First'.
Re: cgi redirect
by keszler (Priest) on Nov 26, 2009 at 14:48 UTC
    From CGI:GENERATING-A-REDIRECTION-HEADER:
    The redirect() method redirects the browser to a different URL. If you use redirection like this, you should not print out a header as well.
      Hi keszler, Thanks for your reply, I tried this "print redirect(-location=>'http://localhost/html/test.html',-nph=>1);" but still it won't redirect.
        Did you still have the print header,start_html,h1('redirecting to a different page'); line before the print redirect?

        CGI's redirect function sends a complete HTTP response to the web browser telling it that the page it requested moved somewhere else, with the URL to there.

        $ perl -le ' use CGI qw/:standard/; print redirect("http://some.where.else"); ' Status: 302 Found Location: http://some.where.else

        It looks like you're trying to redirect while displaying a page to the user. Something like

        <html> <head> <meta http-equiv="REFRESH" content="5;url=http://localhost/html/te +st.html"> </head> <body> <h1>redirecting to a different page</h1> </body> </html>
        CGI's redirect doesn't do that.