You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP Header Location跳转失效求助(无HTTP状态码变更)

Debugging PHP Header Redirect Failure in NGINX + PHP 7.0 Environment

Let’s break down why your header() redirect isn’t working, step through fixes, and answer your lingering questions.

First, the Root Cause of Your Redirect Failure

Looking at your HTTP response data, notice this critical line in the content:


Notice: Undefined index: HTTP_ACCEPT_LANGUAGE in ...

Even though you thought you weren’t seeing errors, this Notice is being output before your header() check runs. Any output—whether errors, warnings, whitespace, or even accidental echo statements—triggers the web server to send HTTP headers immediately. That means by the time your code reaches if(!headers_sent()), headers have already been sent, so the redirect code never executes at all.

Fix Step 1: Eliminate Premature Output

Fix that Undefined index Notice first. In the part of your code where you use $_SERVER['HTTP_ACCEPT_LANGUAGE'], add a safety check to avoid the error:

// PHP 7+ syntax (clean and concise)
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'en-US';

// For older PHP versions:
$lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : 'en-US';

Once this Notice is gone, there won’t be any unexpected output blocking your redirect.

Fix Step 2: Verify Redirect Execution

After fixing the Notice, add a debug line to confirm your redirect code runs:

if(!headers_sent()){
    var_dump("Redirect code is running"); // Temporary debug line
    header("Location: http://google.com");
    exit;
}

If you see that debug message in your response, the redirect header is being sent. If not, double-check all included files and whitespace before <?php tags for hidden output.


Answers to Your Unresolved Questions

1. Difference Between header("Status: 303 ...") and header('HTTP/1.1 301 Moved Permanently')

  • header('HTTP/1.1 301 Moved Permanently');: Sends a full, explicit HTTP status line, hardcoding the protocol version and status code. Use this if you need strict control over the HTTP protocol version.
  • header("Status: 303 See Other");: A PHP shorthand that adapts to your server’s SAPI (e.g., uses HTTP/1.0 for older CGI environments). It sets the status code without locking you into a specific protocol version, making it more flexible across environments.

Both achieve the core goal of triggering a redirect—choose based on whether you need protocol specificity.

2. cgi.rfc2616_headers Configuration

This setting controls how PHP formats HTTP headers:

  • When set to 1: PHP sends headers following the RFC 2616 standard (e.g., HTTP/1.1 302 Found). This is strongly recommended for NGINX + PHP-FPM environments, as NGINX expects standard RFC-compliant headers.
  • When set to 0: PHP uses an old CGI-style format (e.g., Status: 302 Found), which NGINX may not parse correctly.

Check your current value with:

echo ini_get('cgi.rfc2616_headers');

If it’s 0, update it in your php.ini to 1 and restart PHP-FPM.

3. PHP SAPI Type

In an NGINX environment, your PHP SAPI is almost certainly FPM-FCGI (FastCGI Process Manager)—the standard, efficient choice for NGINX. Confirm this by running phpinfo() and looking for the Server API field.

4. Testing Outside Phar Packages

Create a minimal test script outside your Phar package to isolate the issue:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// No extra output before this point
if (!headers_sent()) {
    header("Location: http://google.com");
    exit;
}
echo "Headers were already sent before redirect attempt";
  • If this script redirects successfully: The problem is specific to your Phar package (e.g., included files in the Phar are generating hidden output).
  • If it still fails: Check your NGINX configuration for rules blocking the Location header (see below).

Additional NGINX-Specific Checks

If your minimal test script still doesn’t redirect, check your NGINX server configuration for:

  • fastcgi_hide_header Location;: This directive tells NGINX to strip the Location header sent by PHP, which completely breaks redirects. Remove or comment out this line if present.
  • Cache rules: Ensure NGINX isn’t caching responses that should redirect—add add_header Cache-Control "no-cache, no-store"; to your PHP location block if needed.

内容的提问来源于stack exchange,提问作者ProtectedVoid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:25:45