PHP 使用 URL 变量重定向

PHP redirect with URL variables

我是 php 的新手,正在尝试构建一个使用 URL 参数的重定向,这因人而异。

一个人将访问的 URL 看起来像:www.website1.com/redirect.php?p=p123&r=4&s=567

p、r 和 s 会因人而异

然后我想将他们重定向到其他类似这样的网站:

www.website2.com/p123.aspx?r=4&s=567

这是我目前的情况,但它给我一个错误 "Cannot modify header information - headers already sent by ..."

<html>
   <?php

      $fpvalue  = $_GET['p'];
      $frvalue  = $_GET['r'];
      $fsvalue  = $_GET['s'];

      header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);


   ?>
</html>

非常感谢对初学者的帮助。

谢谢!

当您收到 "content"(文本、html、space,任何地方)时,您无法进行重定向。 在调用 header() 函数之前,您不应该这样做。

如您所见,在调用 header() 函数之前有一个“”。

更改为:

   <html>
   <?php

      $fpvalue  = $_GET['p'];
      $frvalue  = $_GET['r'];
      $fsvalue  = $_GET['s'];

      header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);


   ?>
   </html>

为此:

   <?php

      $fpvalue  = $_GET['p'];
      $frvalue  = $_GET['r'];
      $fsvalue  = $_GET['s'];

      header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);
      exit;
   ?>
   <html>
   </html>

并记住:检查在“< ?php”标签之前是否还有另一个 space 或 "new line"。

当您将 session_start 或 php header 置于其他代码下方时导致的错误 "Cannot modify header information - headers already sent by ...",例如html 那么你应该变成:

<?php
//your php codes
//....
?>

<!DOCTYPE html>
....etc.

这一定有用

此说明适用于我修复 seo 重定向,但你使用所有

示例url:http://yourdomain.com/index2.php?area=xpto

示例index2.php 文件:

<?php 

$fpvalue  = $_GET['area']; 

if ($fpvalue == 'xpto') {
    header("Location: http://newurl.com/", true, 301); 
}

else if ($fpvalue == 'loren') {
    header("Location: http://otherurl.com/", true, 301); 
}   

else if ($fpvalue == '') {
    header("Location: http://otherurl.com/", true, 301); 
}?>