Technologytech

How To Redirect PHP

how-to-redirect-php

To redirect a user in PHP, you use the header() function to send a Location HTTP header to the browser, which instructs it to navigate to a new URL. This server-side method is the correct way to perform a php redirect after a form submission or login, ensuring the page changes without breaking functionality or harming SEO, a practical skill that reinforces 7 reasons why beginner programmers should study php programming language.

How to make a php redirect with the header() function

The primary server-side method for redirecting is the header() function. The correct syntax is header('Location: target-page.php');. After calling header(), you must immediately call exit() or die() to stop further script execution. This prevents unintended code from running and avoids potential security issues. The header() function must be called before any output, such as HTML, whitespace, or echo statements, is sent to the browser. If output starts first, PHP will trigger a "headers already sent" error.

Setting the right HTTP status code for your redirect

If no HTTP status code is explicitly provided with the Location header, PHP defaults to sending a 302 Found (temporary redirect) status code. To implement a permanent redirect, use header("Location: /new-page.php", true, 301); or http_response_code(301); header("Location: /new-page.php");. A 301 redirect signals that the resource has moved permanently, which helps preserve SEO rankings. For temporary redirects, such as during site maintenance or A/B testing, use header("Location: /temporary-page.php", true, 302); or http_response_code(302); header("Location: /temporary-page.php");. A 303 See Other redirect is recommended after processing a form submission to prevent accidental re-submission if the user refreshes the page, as it forces the redirected request to use the GET method. 307 Temporary Redirect and 308 Permanent Redirect status codes are used when the original HTTP request method (e.g., POST) must be preserved during the redirection, which is particularly relevant for APIs.

Fixing 'headers already sent' errors

The "headers already sent" error occurs when output starts before the header() call. This can be caused by whitespace outside PHP tags, echo statements, or HTML before the redirect code. Output buffering, enabled by ob_start() at the script's beginning or via php.ini settings, can prevent these errors by holding output until explicitly flushed or the script ends. Place ob_start() at the very top of your script to capture any accidental output, then call header() and exit() as normal.

Client-side alternatives

Server-side PHP redirects are generally preferred over client-side methods like HTML meta refresh tags or JavaScript redirects for better SEO, faster execution, and improved user experience. HTML meta refresh tags and JavaScript redirects are client-side, execute after page content has loaded, can be slower, and may negatively impact usability and search engine optimization. Client-side methods might be used in specific edge cases, such as when server-side code cannot be modified or when a delayed redirect is needed for a user-facing message. For JavaScript, use window.location.href = 'target-page.php';. For HTML, use <meta http-equiv="refresh" content="0;URL='target-page.php'">.

PHP redirect best practices

Follow this concise checklist for reliable redirects:

  1. Always use exit() or die() immediately after the header() call to stop script execution.
  2. Prefer absolute URLs for external redirects to ensure the browser navigates correctly, especially when the base URL might change.
  3. Avoid redirect loops by verifying that the target URL does not redirect back to the original page.
  4. Test thoroughly on different browsers and devices to confirm the redirect works as expected and the correct HTTP status code is sent.

Sources

The steps on this page were checked against the following documentation. Last verified 16 September 2026.

  1. Bluehosthttps://bluehost.com
  2. Elementorhttps://elementor.com/blog/php-redirect/
  3. Gekkodehttps://gekkode.com
  4. Phppothttps://phppot.com/php/php-redirect/
  5. Promincproductionshttps://promincproductions.com/blog/php-headers-301-redirect-vs-302-redirect/
  6. Pizzahttps://redirect.pizza/resources/redirects/http-redirect-status-codes

About the author

Devora Gorski is the digital age's answer to traditional education, a visionary bridging the gap between technology and learning on Robots.net.

View all 95 articles by Devora Gorski  ·  Our editorial policy

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Stories