简体   繁体   English

PHP使用URL变量重定向

[英]PHP redirect with URL variables

I'm very new to php and trying to build a redirect which uses parameters from a URL which will differ from person to person. 我对php很陌生,并尝试建立重定向,该重定向使用URL中的参数,该参数因人而异。

The URL a person will access looks like: www.website1.com/redirect.php?p= p123 &r= 4 &s= 567 人们将访问的URL如下所示:www.website1.com/redirect.php? p = p123 &r = 4 &s = 567

The p, r, and s will change for each person 每个人的p,r和s都会改变

I then want to redirect them to some other site that looks like this: 然后,我想将它们重定向到如下所示的其他站点:

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

Here is what I have so far, but it's giving me an error "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>

I would really appreciate the help for a beginner. 我真的很感谢初学者的帮助。

Thanks! 谢谢!

You can't do a redirection when you have been sent a "content" (text, html, space, wherever). 当您收到“内容”(文本,html,空格,任何地方)后,就无法进行重定向。 You should NOT do this before calling the header() function. 在调用header()函数之前,请勿执行此操作。

As you can see, you have a "" before calling the header() function. 如您所见,在调用header()函数之前,您有一个“”。

Change that: 更改:

   <html>
   <?php

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

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


   ?>
   </html>

For that: 为了那个原因:

   <?php

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

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

And remember: Check if there is another previous space or "new line" before the "< ?php " tag. 并记住:检查“ <?php”标记之前是否还有另一个先前的空格或“换行”。

The error "Cannot modify header information - headers already sent by ..." caused when you place session_start or php header below other codes, eg html then you should change into: 当您将session_start或php标头放置在其他代码(例如html)下面时,会引起错误“无法修改标头信息-标头已经由...发送”,则应更改为:

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

<!DOCTYPE html>
....etc.

This must work 这必须工作

This instructions works for me to fix seo redirects, but you use for all 本说明对我有用,可以修复seo重定向,但是您可以使用所有

example url: http://yourdomain.com/index2.php?area=xpto 范例网址:http: //yourdomain.com/index2.php?area = xpto

example index2.php file: 示例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); 
}?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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