How do I make a redirect in PHP?
Categories:
Mastering PHP Redirects: A Comprehensive Guide

Learn how to implement various types of HTTP redirects in PHP, including 301, 302, and JavaScript-based methods, ensuring proper user and SEO handling.
Redirects are a fundamental part of web development, allowing you to guide users and search engines from one URL to another. Whether you're moving content, fixing broken links, or implementing A/B testing, understanding how to properly implement redirects in PHP is crucial. This article will cover the most common methods for performing redirects, focusing on server-side (PHP) and client-side (JavaScript/Meta Refresh) techniques, along with best practices for each.
Understanding HTTP Redirects
HTTP redirects are server responses that tell a web browser or search engine crawler that the requested resource has moved to a different URL. They are categorized by status codes, which indicate the nature of the move. The most common are 301 (Moved Permanently) and 302 (Found/Moved Temporarily).
flowchart TD A[User Request URL_A] --> B{Server Receives Request} B --> C{PHP Script Executes} C --> D{PHP Sends HTTP Header (e.g., 301/302)} D --> E[Browser Receives Header] E --> F[Browser Redirects to URL_B] F --> G[User Sees URL_B Content]
Flow of a server-side PHP redirect
Server-Side Redirects with PHP's header()
Function
The header()
function in PHP is the primary way to send raw HTTP headers, including those for redirects. It's crucial that header()
is called before any actual output is sent to the browser (e.g., HTML, whitespace, or echo
statements), otherwise, you'll encounter a 'Headers already sent' error.
<?php
// Permanent Redirect (301 Moved Permanently)
header("Location: https://www.example.com/new-page.php", true, 301);
exit();
// Temporary Redirect (302 Found)
// header("Location: https://www.example.com/temporary-page.php", true, 302);
// exit();
// Other common temporary redirect (307 Temporary Redirect)
// header("Location: https://www.example.com/another-temp-page.php", true, 307);
// exit();
?>
Implementing 301 and 302 redirects using PHP's header()
function.
exit();
after a header('Location: ...')
call. This stops script execution immediately, preventing further code from running and potentially sending unwanted output or causing security vulnerabilities before the redirect occurs.Choosing the Right HTTP Status Code
The choice between a 301 and 302 redirect is critical for SEO and browser caching. A 301 indicates that the resource has permanently moved, telling search engines to transfer link equity (PageRank) to the new URL and browsers to cache the redirect. A 302 (or 307) indicates a temporary move, meaning search engines should not update their index and browsers should not cache the redirect.

Key differences between 301, 302, and 307 redirects.
Client-Side Redirects: JavaScript and Meta Refresh
While server-side redirects are generally preferred, client-side redirects can be useful in specific scenarios, such as when you don't have server access or need a slight delay before redirection. However, they are less SEO-friendly and can sometimes lead to a poorer user experience.
JavaScript Redirect
If you are not redirected automatically, follow this link.
Meta Refresh Redirect
You will be redirected in 5 seconds. If not, click here.
Best Practices for PHP Redirects
To ensure your redirects are effective and don't negatively impact your site, follow these best practices:
1. Use Absolute URLs
Always use full, absolute URLs (e.g., https://www.example.com/new-page.php
) in your Location
header, not relative paths. This prevents unexpected behavior and ensures the redirect works correctly regardless of the current page's path.
2. Call exit()
After header()
As mentioned, exit()
or die()
should always follow a header('Location: ...')
call to prevent further script execution and potential issues.
3. Avoid 'Headers Already Sent' Errors
Ensure no output (HTML, whitespace, echo
, print_r
, etc.) is sent to the browser before the header()
function is called. If you encounter this error, check for BOM characters, leading/trailing whitespace in PHP files, or early echo
statements.
4. Log Redirects (Optional but Recommended)
For complex sites, consider logging redirects to monitor their usage and identify any broken redirect chains or loops.
5. Test Thoroughly
Always test your redirects using browser developer tools (network tab) or online HTTP header checkers to confirm the correct status code and destination URL are being sent.