How do I make a redirect in PHP?

How do I make a redirect in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Server-Side Redirects in PHP

When a user requests www.example.com/page.php but you intend to serve them content from index.php, relying on client-side techniques like meta refresh tags is generally a bad idea. Meta redirects are slow, they can be blocked by assistive technologies, and crucially, they do not count as an actual navigation event in the browser's history for security-sensitive operations. The robust solution lies entirely within PHP using HTTP status codes.

The most effective method to handle this redirection is sending a Location header followed immediately by terminating the script execution. This approach forces the browser to make a new request to the target URL without rendering the original page content first. You achieve this using the header() function combined with exit().

Here is the standard implementation:

<?php
// Define the destination URL
$destination = 'https://www.example.com/index.php';

// Send the redirect header
// Status code 302 indicates a temporary redirect, though 301 (permanent) is often preferred for SEO
header('Location: ' . $destination);

// Terminate script execution immediately
exit();

The logic here is straightforward. The header() function injects the HTTP header into the response stream. Once PHP outputs this specific header with a status code of 302 or 301, the browser parses it and initiates a new request to the provided URL. The subsequent exit() call is non-negotiable; if omitted, PHP will attempt to continue executing and might output HTML content before the redirect logic finishes, resulting in confusing warnings or broken behavior.

Beyond simple URL changes, this mechanism is vital for access control. You can use these redirects to enforce authentication requirements seamlessly. If a user tries to access a protected resource like page.php without being logged in, you can redirect them to your login page. This effectively hides the existence of the protected file from unauthorized users, as the server never returns the content of the file itself.

<?php
session_start();

// Check if the user is authenticated
if (!isset($_SESSION['is_logged_in']) || $_SESSION['is_logged_in'] !== true) {
    header('Location: https://www.example.com/login.php');
    exit();
}

// If not redirected, process the request normally
include 'page_content.php'; 

When deciding between a 301 (Moved Permanently) and a 302 (Found or Temporary Redirect), the context matters. A 301 is typically used when a page has moved permanently; it signals search engines to update their indexes and allows browsers to cache the redirect for performance. A 302, however, implies the move is temporary. If you are using redirects solely for session management—such as forcing a login before accessing a dashboard—a 302 is often more appropriate because the user's state changes frequently.

It is also important to consider the order of operations. The header() function must be called before any output, including whitespace or BOMs, is sent to the browser. If you see a "headers already sent" warning in your error logs, it means PHP encountered output (like an echo statement or a stray newline) before the redirect command. Debugging this often involves checking for invisible characters at the top of files or ensuring no syntax errors precede the header call.

For developers working within the Laravel ecosystem, you might find yourself implementing similar logic using the framework's HTTP kernel. While raw PHP offers fine-grained control, Laravel provides helpers like redirect() which handle the underlying headers and status codes automatically. If you are migrating legacy authentication flows to a modern stack, understanding these raw mechanics ensures that custom middleware or gateways behave predictably even when stepping outside the framework's conventions.

Our network