如何重定向
example.com/script.php?id=567
到
example.com/script2.php?id=5677&something=anotherthing&something2=anotherthing2
在php中使用标题301重定向?
答案 0 :(得分:3)
代码本身很简单:
<?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();
}
?>
您还可以使用$_SERVER['HTTP_HOST']获取主机名example.com
。在调用header()
之前,您还必须确保您的脚本没有任何输出。
答案 1 :(得分:1)
如果您有权访问文件script.php
,则可以在顶部添加以下代码:
<?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();
?>