4

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!

1

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.