The Critical Difference Between 301 and 302 in PHP Redirection Logic
Choosing between a 301 (Moved Permanently) and a 302 (Found) redirect is often treated as a trivial checkbox decision, yet it fundamentally alters how search engines crawl your site and how browsers cache requests. When planning a website launch or a maintenance window, the status code you emit dictates whether Google treats your URLs as broken or temporarily unavailable.
In your proposed PHP snippet, you are currently sending users to either thank-you.html or home-page.html. The critical question is not just where they go, but how the browser and server interpret that movement. If you are showing a "down for maintenance" page while keeping the rest of the site accessible (or if you intend to temporarily change all traffic to a specific landing page), you must use a 302 status code. A 301 tells search engines that the URL has permanently moved, prompting them to update their index and transfer link equity to the new location. Doing this during a launch phase or maintenance window is disastrous for SEO; you will lose rankings because Google will assume your content no longer exists at the original path.
Implementing Correct Status Codes in PHP
PHP's header() function allows you to send HTTP headers, but it does not automatically append a status code unless explicitly told to do so. The default behavior of header('Location: ...') is to send a 302 redirect. While this defaults correctly for temporary moves, relying on the default is risky in production environments where configuration nuances can sometimes alter behavior.
To ensure search engines and browsers receive the exact signal you intend, you should explicitly define the status code before sending the Location header. Here is the robust pattern for a temporary maintenance window:
<?php
session_start(); // Optional, but ensures session data isn't lost on redirect if needed
$visitor = $_SERVER['REMOTE_ADDR'];
$targetUrl = 'http://www.yoursite.com/home-page.html';
// Explicitly set the 302 status before sending any output
http_response_code(302);
if (preg_match("/192\.168\.0\.1/", $visitor)) {
// Admin sees the maintenance page or a specific internal URL
header("Location: http://www.yoursite.com/thank-you.html");
} else {
// Public users see the standard landing page
header("Location: $targetUrl");
}
exit();
?>
Why Explicit http_response_code() Matters
Some hosting environments or reverse proxies might normalize headers, but explicitly calling http_response_code(302) guarantees that the HTTP response line begins with HTTP/1.1 302 Found. This is distinct from a 301, which would be HTTP/1.1 301 Moved Permanently.
If your goal is to show a "under construction" message directly on the requested URL rather than redirecting users away entirely, you should not use the Location header at all. Instead, you serve the maintenance page content and return a 200 OK status:
<?php
http_response_code(200); // Explicit success status
?>
<!DOCTYPE html>
<html>
<head><title>Scheduled Maintenance</title></head>
<body>
<h1>We are currently updating our site.</h1>
<p>Please check back later.</p>
</body>
</html>
Using a 200 status with a custom error page is generally preferred for maintenance windows because it allows you to serve the content directly. However, if your specific requirement is to force all traffic to a new URL (like home-page.html) while keeping the old URLs valid in the index temporarily, the explicit 302 redirect shown above is the correct approach.
Configuration Alternatives: The .htaccess Approach
You asked if there is an <code>.htaccess</code> based approach. This is often a better choice for performance and simplicity. Apache handles redirects at the server level before PHP ever processes the request, reducing CPU overhead.
If you decide to stick with the maintenance logic but prefer an .htaccess file, you can use mod_rewrite to conditionally redirect based on IP address:
RewriteEngine On
# Redirect everyone except 192.168.0.1/32 to home-page.html
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com$
RewriteCond %{REMOTE_ADDR} !^192\.168\.0\.1$
RewriteRule ^(.*)$ http://www.yoursite.com/home-page.html [R=302,L]
# Optional: Serve a specific file for the admin IP without redirecting
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com$
RewriteCond %{REMOTE_ADDR} ^192\.168\.0\.1$
RewriteRule ^.*$ /maintenance.html [L]
Final Considerations for Launches
When executing this logic during a launch phase, precision is key. A 301 redirect during a temporary outage signals to Google that the original content has vanished. If you switch to a permanent redirect now, you risk losing organic traffic permanently. Stick to the 302 status code until the maintenance window or launch transition is truly complete and you are ready for the new URL structure to become permanent.
For developers managing complex routing logic within modern PHP frameworks, ensuring that your framework's router respects these HTTP status codes is vital. Frameworks like Laravel provide helpers to manage redirects cleanly while maintaining strict control over status codes. For more advanced routing patterns and deployment strategies in PHP applications, resources like https://laravelcompany.com offer insights into best practices for handling middleware and response codes effectively.
