
3 minutes read
Redirect to another page in PHP
- → Introduction
- → Quick PHP redirect example (TL;DR)
- → HTTP headers explained
- → Absolute vs. relative URLs
- → Choosing the correct HTTP status code
- → SEO considerations: redirect chains and caching
- → Security tip: avoiding open redirects
- → Clearer PHP redirection with http_response_code()
- → Verifying redirects easily
Introduction
Performing a redirect in PHP is straightforward, but mastering redirects involves understanding HTTP status codes, avoiding common pitfalls, and keeping SEO considerations in mind.
Quick PHP redirect example (TL;DR)
Here’s the essential PHP redirect code snippet:
// Start output buffering to avoid "headers already sent" errors. ob_start(); header('Location: https://example.com', true, 301); // Permanent redirect. exit;
Let’s break down what’s happening here:
- Output buffering (
ob_start()
): Ensures nothing is sent to the browser prematurely, preventing the infamous PHP “headers already sent” error. header()
function: Tells browsers to redirect to the specified URL.- HTTP status code (301): Indicates this redirect is permanent, beneficial for SEO.
exit
: Stops further PHP execution immediately after redirection.
HTTP headers explained
When you redirect with PHP, the server sends an HTTP response similar to this:
HTTP/1.1 301 Moved Permanently Location: https://example.com Content-Type: text/html
The important elements:
- Status code (301): Informs browsers and search engines this URL has moved permanently.
- Location header: Indicates the new URL destination.
Avoid echoing text before sending headers. If you accidentally output whitespace or content before calling header()
, PHP throws the “headers already sent” error. Output buffering (ob_start()
) is a reliable safeguard against this.
Absolute vs. relative URLs
Always use absolute URLs (https://example.com/page
) in PHP redirects. Relative URLs (/page
) typically work but can fail with certain proxies or older browsers, causing unpredictable results.
Choosing the correct HTTP status code
PHP defaults to a 302 Found
redirect if you don’t specify a status code explicitly:
header('Location: https://example.com'); // defaults to 302
For better SEO, explicitly choose the correct status code:
- 301 Moved Permanently: Permanent move, ideal for SEO. Aggressively cached by browsers and search engines.
- 302 Found: Temporary redirect, rarely cached.
- 303 See Other: Redirect after form submission (PRG pattern).
- 307 Temporary Redirect: Temporary, preserves original HTTP method (GET/POST).
- 308 Permanent Redirect: Permanent, preserves HTTP method.
Example:
// Temporary redirect, preserves POST method. header('Location: https://example.com/temp-page', true, 307); exit;
SEO considerations: redirect chains and caching
Googlebot follows a maximum of 10 redirect hops. Long chains negatively affect SEO and performance. Consolidate redirect chains to fewer steps.
Also, permanent redirects (301
and 308
) are aggressively cached by browsers and CDNs. If you anticipate future changes or conduct A/B tests, prefer temporary codes like 302
or 307
.
Every redirect adds latency, potentially harming your site’s Core Web Vitals, which remain essential to Google’s ranking signals in 2025. Optimize your redirects to minimize delays.
Security tip: avoiding open redirects
Avoid redirecting to URLs directly from user input without validation, as this can lead to phishing attacks.
Secure example with a URL whitelist:
$allowed_urls = ['/dashboard', '/profile', '/home']; $next = $_GET['next'] ?? '/home'; if (!in_array($next, $allowed_urls, true)) { $next = '/home'; } header("Location: $next", true, 303); exit;
Clearer PHP redirection with http_response_code()
For better readability, you can set the HTTP status code explicitly:
ob_start(); http_response_code(301); header('Location: https://example.com'); exit;
Verifying redirects easily
To verify your redirects, use curl
from the command line:
curl -I https://your-site.com/old-page
Expected output:
HTTP/1.1 301 Moved Permanently Location: https://your-site.com/new-page
Alternatively, browser developer tools clearly show redirection details.
With these practices, your PHP redirects will be reliable, secure, SEO-friendly, and high-performing.
Did you like this article? Then, keep learning:
- Explore PHP 8.4's new features for up-to-date coding practices
- Know how to find out your PHP version quickly and easily
- Learn how to convert PHP arrays to JSON for APIs and data exchange
- Master handling PHP exceptions using try and catch blocks
- Master printing arrays in PHP for better debugging experiences
- Improve your code with PHP's null coalescing operator explained
- Learn different types of PHP redirections beyond basics
- Debug PHP code effectively by showing all errors plainly
- Understand and solve common PHP errors like $this in non-object context
0 comments