Redirecting Browser to a URI

This section provides a tutorial example on how to redirect the browser to go to a new URI with the redirect() method in the CGI.pm module.

CGI.pm also allows us to set a redirect URI in the HTTP response header. This tells the browser to make a subsequent request with the redirect URI to get the response content. This technique is used commonly by Web servers to inform browsers that a Web page has been moved to a new URI address.

The CGI.pm module offers the redirect() method to set the redirect URI to the returning HTTP response header. Here is a simple example Perl CGI script: CGI-pm-Redirect-URI.pl:

#!c:/local/perl/bin/perl.exe
#- CGI-pm-Redirect-URI.pl
#- Copyright (c) 2014, HerongYang.com, All Rights Reserved.
   
   use CGI;
   use CGI::Cookie;
   
   $query = CGI->new();

#- Getting getting the redirected URI as a parameter from the request
   $uri = $query->param('uri');
   
   if ($uri) {

#-    Setting redirect URI into the response
      print $query->redirect(-uri=>$uri, -status=>'302 Found');
   } else {
      print $query->header();
      print $query->start_html(-title=>'CGI-pm-Redirect-URI.pl Failed');
      print $query->p('Not redirected. Missing "uri" parameter.');
      print $query->end_html();
   }

Copy CGI-pm-Server-Info.pl to C:\local\apache\htdocs.

First, enter this URI into your browser address: http://localhost/CGI-pm-Redirect-URI.pl. You should see the following message with no redirect.

Not redirected. Missing "uri" parameter.

Next, enter this URI into your browser address: http://localhost/CGI-pm-Redirect-URI.pl?uri=http://www.herongyang.com/. You should see that the browser is redirected my personal home page.

Last update: 2014.

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

CGI.pm Module for Building Web Pages

 What Is CGI.pm?

 Generating HTML Document with CGI.pm

 Retrieving Query Parameters and Headers

 Retrieving Environment and Script Information

Redirecting Browser to a URI

 "Refresh" http-equiv Meta Tag

 What Is CGI::Cookie?

 Retrieving and Setting HTTP Cookies

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version