簡體   English   中英

如何在PHP中使用301標頭重定向頁面?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM