How do I redirect to another page?

Specify the complete URL of the destination (even if it is on the same server). This is one of the two different kinds of CGI "Location:" responses which are defined in the CGI specification for a Parsed Headers script. The other kind (an absolute URLpath) is resolved internally to the server without any HTTP redirection. The CGI specifications do not allow relative URLs in either case.

Use of CGI.pm is strongly recommended. This example shows redirection with a complete URL. This redirection is handled by the web browser.

      use CGI qw/:standard/;
      my $url = 'http://www.cpan.org/';
      print redirect($url);
This example shows a redirection with an absolute URLpath. This redirection is handled by the local web server.

      my $url = '/CPAN/index.html';
      print redirect($url);
But if coded directly, it could be as follows (the final "\n" is shown separately, for clarity), using either a complete URL or an absolute URLpath.

      print "Location: $url\n";   # CGI response header
      print "\n";                 # end of headers

Back to perlfaq9