2

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.

The URL a person will access looks like: www.website1.com/redirect.php?p=p123&r=4&s=567

The p, r, and s will change for each person

I then want to redirect them to some other site that looks like this:

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!

Rob
  • 23
  • 1
  • 1
  • 4
  • There is already good answer on SO for this issue http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – ofca Mar 01 '15 at 22:24

3 Answers3

7

You can't do a redirection when you have been sent a "content" (text, html, space, wherever). You should NOT do this before calling the header() function.

As you can see, you have a "" before calling the header() function.

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.

Broda Noel
  • 1,760
  • 1
  • 19
  • 38
2

The error "Cannot modify header information - headers already sent by ..." caused when you place session_start or php header below other codes, e.g. html then you should change into:

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

<!DOCTYPE html>
....etc.

This must work

Joe Kdw
  • 2,245
  • 1
  • 21
  • 38
0

This instructions works for me to fix seo redirects, but you use for all

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

example index2.php file:

<?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); 
}?>