FAQ 9.11 How do I redirect to another page?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This is an excerpt from the latest version perlfaq9.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .


9.11: 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

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


The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in

Site Timeline