简体   繁体   中英

How can I redirect a page with a 301 header in PHP?

How 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();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM