How can I redirect a page with a 301 header in PHP?
Ho开发者_开发百科w can I redirect
example.com/script.php?id=567
to
example.com/script2.php?id=5677&something=anotherthing&something2=anotherthing2
in php using a header 301 redirect?
The code itself is simple:
<?php
if($_SERVER['REQUEST_URI']=='/script.php?id=567'){
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: http://example.com/script2.php?id=5677&something=anotherthing&something2=anotherthing2');
die();
}
?>
You can also use $_SERVER['HTTP_HOST'] to get the hostname example.com
. And you also have to make sure your script doesn't have any output before calling header()
.
If you have access to the file script.php
then you can add the following code on top:
<?php
$id = $_GET['id'];
//Get your extra params from the database if needed...
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: http://example.com/script2.php?id='.$id.'&something=anotherthing&something2=anotherthing2'); //Append params retrieved from database here.
die();
?>
精彩评论