...

View Full Version : 301 redirect with php



tokio
05-05-2009, 08:01 PM
Anyone have any personal recommendations on how to do this? I searched online and saw that there are lots of ways to do it so im confused on which route to take.

devinemke
05-05-2009, 08:28 PM
header('location: http://www.example.com');
exit();

eak
05-06-2009, 03:46 PM
devinemke, that code does a 302 redirect, not a 301.

tokio, this is what I use:

function redirect( $url, $permanent = false ){
if( parse_url( $url ) !== parse_url( $_SERVER['REQUEST_URI'] ) ){
if( $permanent === true ){
header("HTTP/1.1 301 Moved Permanently");
header('location: ' . $url, true, 301 );
} else {
header('location: ' . $url );
}
exit;
}
return false;
}

By default, the redirect is not permanent.
To make it a 301 redirect, set the second argument to true.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum