Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Redirecting URL in Perl (http://www.go4expert.com/articles/redirecting-url-perl-t15455/)

pradeep 10Dec2008 13:34

Redirecting URL in Perl
 
A very common technique used by many developers is to redirect the user to another URL or the referrer URL after doing some work e.g. processing a form. In Perl all headers have to printed before any actual content is outputed.

A very simple way of redirecting to another URL
Code: Perl

#!/usr/bin/perl

print "Content-Type: text/html\n";
print "Location: http://www.go4expert.com/no-page-here.html\n\n";


Doing the same thing using the CGI module.
Code: Perl

#!/usr/bin/perl

use CGI;

my $cgi = new CGI;

print $cgi->header(-location => q[http://www.go4expert.com/no-page-here.html]);


You may also pass the HTTP status code, sometimes it's needed e.g. if you pass a 302 status code, the URL of the page in the browser remains the same. Here's all the status codes used for redirecting a page.

Code:

301 Moved Permanently
302 Found
303 See Other

So, here's how we redirect with the status.
Code: Perl

#!/usr/bin/perl

use CGI;

my $cgi = new CGI;

print $cgi->header(-location => q[http://www.go4expert.com/no-page-here.html], -status=>301);


shabbir 6Jan2009 12:25

Re: Redirecting URL in Perl
 
Nomination for article of the month for December 2008 Started.

dsouza 10Jan2009 16:00

Re: Redirecting URL in Perl
 
nice post

fazalca 17Apr2009 23:46

Re: Redirecting URL in Perl
 
print redirect('URL');

will do the same thing


All times are GMT +5.5. The time now is 06:58.