Thursday, August 14, 2008

How To Do HTTP 301 Redirection using PHP and Perl

At times, a permanent HTTP 301 redirection is needed to redirect a website location to another. Permanent redirection via HTTP 301 is advised on website redirection where an old link or website needs to be redirected to another website domain or website location.

In short, HTTP 301 code tells user-agents that the current location has been permanently moved somewhere else.

Here's a quick tip on how to do accomplish HTTP 301 Redirection using PHP and Perl

HTTP 301 Permanent Redirection using PHP

Simply, create an index PHP file with the below contents

# vi index.php

<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.ilovetux.com/");
exit();
?>


Save, exit and upload to webserver.

To above example, when browsed from internet browser, redirects an old domain to a new domain. This is very useful on moving from one domain name to another domain permanently. Alternatively, you can append any default HTML file to new domain location to suit your needs.

HTTP 301 Permanent Redirection using Perl


Create a perl file and call it inside the default web page

# vi redirect.pl

#!/usr/bin/perl -w
use strict;
print "Status: 301 Moved Permanantly\n";
print "Location: http://ilovetux.com/newpage.htm\n\n";
exit;


Save, exit and upload the proper web server location.

The above perl redirect example redirects an old page to a new page location using the same HTTP response code. The above lines can be included and called from the default web page such as index.html

All is done.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy