Perl redirect - a Perl CGI redirect example

Perl CGI redirect FAQ: Can you share some Perl CGI redirect example code?

Here's the source code for a simple Perl CGI redirect example. An HTML redirect is pretty easy to program manually, but the Perl module makes it even easier to redirect a user from one page to another.

Perl redirect CGI script example

Here's the source code for this Perl CGI redirect example script:

#!/usr/bin/perl -Tw
#  PROGRAM:  redirect.cgi
#  PURPOSE:  A simple Perl redirect CGI example.
#            Demonstrates how to generate a simple redirect request 
#            for the remote browser that accesses this CGI program.
#  Created by alvin alexander,

#  1. Create a new Perl CGI object  #

use CGI;
$query = new CGI;

#  2. Issue the redirection request.  (Always use an 'absolute' URL.)  #

print $query->redirect('');

(I was going to share a redirect example here, but it wouldn't be too exciting, as it will just redirect you from the given URL to the URL I specify. I'll have to leave that as "an exercise for the reader." :)

Make your Perl redirect script executable

When I first wrote this blog post I forgot to note that you need to make your Perl CGI redirect script executable for it to work, at least on a Unix or Linux system. If you haven't done this before, see the second comment below for an example of how to make your script executable.

Other than that, I hope this Perl redirect CGI example has been helpful.



Yeah this does not actually work... I have tried like every possible example for the redirect function and I can not get it working....

I just tested it here, and also got an error message at first. The one thing I forgot to write here was that at least on a Unix or Linux system, you have to make the script executable. I put my script in the cgi directory, named it foo.cgi, and then after running this command it worked as advertised:

chmod +x foo.cgi

This also assumes that your Apache CGI directory is configured properly, but that's another story. If it still doesn't work, check your Apache error log. In my case, my error message looked something like this:

(13)Permission denied: exec of '/cgi-bin/foo.cgi' failed