[英]How can I redirect a page with a 301 header in PHP?
我该如何重定向
example.com/script.php?id=567
至
example.com/script2.php?id=5677&something=anotherthing&something2=anotherthing2
在PHP中使用标题301重定向?
代码本身很简单:
<?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()
之前,还必须确保脚本没有任何输出。
如果您有权访问文件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();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.