#!/usr/local/bin/perl
### redirect_moved.pl
#################################################################
# This script will send out a proper error code to the caller's
# browser, and will then redirect the caller in one second to
# the URL you provide.
#
# If a search engine crawler comes around, this error will also
# be sent, and then the crawler knows your data has moved or
# is not there anymore. A crawler should then come by at a
# later date and crawl your site again, picking up the new
# information. Users of the search engine should then
# only be sent to your new location.
#
# The way I called this script is to capture the error
# using a .htaccess file. Place this line in a .htaccess
# file, as an example for the 404 error. (without the
# "#" of course)
#
# ErrorDocument 404 /cgi-bin/redirect_moved.pl
#
# You can use any other perl stuff you want, for example, you
# could modify the URL that is redirected to by using the
# $ENV{HTTP_REFERER} information. Make sure you filter
# input properly, I included two subroutines at the end
# that you may find handy for cleaning input and creating
# a properly formated URL.
#
# You can use or modifiy this free of charge so long as
# this notice and the comments remain intact. By using this
# code you agree to indemnify No Crash or it's owners from any
# liability that might arise from it's use.
#
# Selling the code for this program without prior written
# consent is expressly forbidden.
#
# For more examples and free sources, visit www.nocrash.com
#################################################################
print "Content-type: text/html\n";
# if you need to know the status for some reason
$status = $ENV{'REDIRECT_STATUS'};
# you need to change this line depending on what error you are reporting
# most systems just look at the number, but you should have the
# text correct anyway. Users don't see this on their screen.
print "Status: 404 Not Found\n\n";
# users see this on their screen at the top
$page_title = "404 Not Found";
# put your new URL here
$reurl = "http://www.yourdomain.com/new_whatever.html";
# this is the way we redirect in one second
$meta_tag = "";
# all this is sent to the browser, you don't have to get this fancy
# but you should send out at least 512 bytes so that IE will not show
# it's default error page.
print "\n";
print "
\n";
print " $meta_tag\n";
print " $page_title\n";
print " \n";
print " \n";
print "\n";
print "
\n";
print "\n";
print "\n";
print "\n";
print "\n";
print "\n";
print " \ $page_title\n";
print " | \n";
print " \n";
print " \n";
print "\n";
print "\n";
print "\n";
print "\n";
print "Page not found.\n";
print " \n You should be transfered to the ";
print "";
print "NEW PAGE shortly. If not, ";
print "";
print "Click Here to go to there.\n";
print "\n";
print "\n";
print " | \n";
print " \n";
print " \n";
print " | \n";
print "
\n";
print "
\n";
print <
Copyright © 2005 Your Company Inc. All Rights Reserved.
EOF
exit;
sub parse_referrer {
local($name,$value);
# Get the input
if ($ENV{'HTTP_REFERER'}) {
$refer = $ENV{'HTTP_REFERER'};
# this helps parse first thing from URL
$refer =~ tr/?/&/;
# Split the name-value pairs
@pairs = split(/&/, $refer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
# Un-Webify plus signs and %-encoding
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\0//g; ## Remove any NULL characters
$value =~ s///g; ## security - dont allow server side includes!
$value =~ s/<([^>]|\n)*>//g; ## take out any html
$REFERER{$name} = $value;
}
}
}
sub url_encode {
my $text = shift;
$text =~ s/([^a-z0-9_.!~*'( ) -])/sprintf "%%%02X", ord($1)/ei;
$text =~ tr/ /+/;
return $text;
}