How do I make a redirect in PHP?

Use the header() function to send an HTTP Location header:

header('Location: '.$newURL);

Contrary to what some think, die() has nothing to do with redirection. Use it only if you want to redirect instead of normal execution.

File example.php:

<?php
    header('Location: static.html');
    $fh = fopen('/tmp/track.txt', 'a');
    fwrite($fh, $_SERVER['REMOTE_ADDR'] . ' ' . date('c') . "\n");
    fclose($fh);
?>

Result of three executions:

bart@hal9k:~> cat /tmp/track.txt
127.0.0.1 2009-04-21T09:50:02+02:00
127.0.0.1 2009-04-21T09:50:05+02:00
127.0.0.1 2009-04-21T09:50:08+02:00

Resuming — obligatory die()/exit() is some urban legend that has nothing to do with actual PHP. It has nothing to do with client “respecting” the Location: header. Sending a header does not stop PHP execution, regardless of the client used.

5 thoughts on “How do I make a redirect in PHP?”

  1. I was suggested this blog by my cousin. I am not sure whether this post
    is written by him as no one else know such detailed about my difficulty.
    You’re incredible! Thanks!

    Reply
  2. An impressive share! I have just forwarded this
    onto a co-worker who was conducting a little homework on this.
    And he actually ordered me breakfast simply because I
    found it for him… lol. So allow me to reword this….
    Thanks for the meal!! But yeah, thanx for spending time to talk
    about this matter here on your internet site.

    Reply
  3. Hi there to every one, the contents present at this web page are in fact awesome for people
    knowledge, well, keep up the nice work fellows.

    Reply

Leave a Comment