如何在PHP中重定向[重复]

This question already has an answer here:

File redirect.php contain this code:

   <?php

$url  = $_SERVER["PHP_SELF"];
$path = explode("?q=", $url); 
$last = end($path);
header('Location: http://mywebsite2.com/something&q='.$last);
?>

However, when I go http://mywebsite.com/redirect.php?q=anything, it shows an error:

Cannot modify header information - headers already sent by".

How to fix it?

</div>

Looks like you have some whitespace before the opening <?php tag. This will cause the error.

   <?php
^^^ remove

Header Must be FIrst HTML ELEMENT in the document Remember spaces do count as html elements

remove them

header('Location: http://mywebsite2.com/something&q=anything');
die():

for more

PHP variable in header function

EDITED

ADD ANYTHING IN YOUR URL :) SImple

header('Location: '.$yourURL);

or you can use

header("Location: yourPage.php");
die();

Why you should use die() go to this link