PHP Redirect Code
Generate a PHP header redirect with the right status code and the exit call that people forget, which is what causes the page to keep executing.
How to use it
- Enter the destination — and the old path where the method needs one.
- Choose 301 for a permanent move or 302 for a temporary one.
- Copy the generated code into the right place for your server or page.
header("Location: ...") sends an HTTP redirect straight from server-side PHP, before any HTML has been output to the browser — which is exactly what makes it a proper redirect rather than the client-side workaround a meta refresh or JavaScript redirect is. This generator sets the status code explicitly, 301 for permanent or 302 for temporary, as the second argument to header(), and adds exit immediately after it.
Why exit has to follow header()
header() only queues the response header — it does not stop the script. Without an exit call directly after it, PHP keeps executing everything below, which can output content the browser ignores because it already followed the redirect, or worse, run further logic — a database write, an email send — that you did not intend to fire on a page the visitor never actually saw.
The "headers already sent" error
This happens when anything is output before header() runs — and “anything” includes things that are easy to miss, like a blank line or a space before the opening <?php tag, or a stray echo earlier in an included file. Once PHP has sent even a byte of output, the response headers are locked and header() fails.
The fix is to make sure the redirect logic runs before any output whatsoever, ideally at the very top of the script, or to use PHP's output buffering (ob_start()) so nothing is actually flushed to the browser until you choose to send it.
Questions people ask
Why do I get “headers already sent”?
Something was output before header() ran — even a blank line before <?php counts. Move the redirect above all output, or use output buffering.
Why is exit needed after header()?
Because header() only queues the redirect. Without exit, the rest of the script still runs and can leak content or perform actions you did not intend.
Is anything uploaded?
No. Everything runs in your browser — no file, image or snippet you use here ever leaves your device.
Last updated 22 August 2026