Monday, October 18, 2010

Redirecting in PHP and Apache

There are a couple of ways to redirect a webpage.  The first consideration is whether this is a permanent or a temporary redirect.  Each has its own HTTP code.  Permanent is 301 and temporary is 302.

Permanent Redirect in PHP

<?php 
    header( "HTTP/1.1 301 Moved Permanently" ); 
    header( "Location: http://www.example.com/newpage.php" ); 
    die(); 
?>

Temporary Redirect in HTML


<META HTTP-EQUIV="refresh" CONTENT="10; URL=http://example.com/newpage.php">

It delays 10 seconds before taking them to the new page, allowing them to see the contents of your page before going to the new page.

Permanent Redirect in Apache


Add the following line to httpd.conf

Redirect permanent /foo http://www.example.com/bar


You can redirect a directory, a single page, or the entire site.


No comments:

Post a Comment