#!/usr/bin/perl
# Perl script redirect to another http/ftp url or a page 
# You can call perl script as follows:
#
# A) Redirect to url cyberciti.biz 
# http://mydomain.com/cgi-bin/rd?url=http://cyberciti.biz/
#
# B) To call perl script from html page as javascript 
# (put in your html file, note o=js option at the END):
# <script language="JavaScript" type="text/javascript" 
#   src="http://mydomain.com/cgi-bin/rd.pl?url=http://cyberciti.biz/&o=js"
# </script>
# -----------------------------------------------
# Copyright (c) 2005 Vivek G Gite <http://cyberciti.biz/fb/>
# This perl script is licensed under GNU GPL version 2.0 or above
# ------------------------------------------------

use strict;
use warnings;
use CGI;

# new CGI 
my $q = CGI->new( );
my $error;
# get url param
my $rdurl = $q->param("url");

# output can be 
# js - for javascript, to create javascript based redirection so you can use script from HTML pages
# make sure o param is in lowercase :)
my $output = lc($q->param("o")); 

# make sure url passed as url=http://somewhere.com/page.html
if ( $q->param('url') eq "" ){
  print $q->header();
  print $q->start_html(-title=>"Error URL missing");
  $error = "URL missing, you must run this script http://" .  $ENV{'SERVER_NAME'} . $ENV{'SCRIPT_NAME'} ;
  $error .= "?url=http://abc.com/somepage.html<BR>";
  $error .= "I will redirect a page/url to http://abc.com/somepage.html";
  print $error;
  print $q->end_html();
}
else{ # redirect to a page 
    if ( $output eq "" || $output eq "html" ){
       print $q->redirect( -URL => $rdurl);
    }
    else { 
      # do javascript based redirection / a page
      # Send fresh header or sky will fall on you :P
      print $q->header(); 
      # do a page redirection with javascript 
      print "window.location=\"$rdurl\";\n\n";
    }
}


[ Back | Home | Download script (1.8K) | Discuss/ask more @ perl scripting forum ]
This code licensed under GNU GPL version 2.0 or above, every effort has been made to ensure all information and examples (soruce code) are correct BUT no guarantee is implied or intended, read our disclaimer and privacy policy.
Copyright © 2005 Vivek G Gite.