Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

header() for a 404 does nothing in PHP: the page still shows 200

ErrorThe browser network panel shows 200 OK even though header('HTTP/1.0 404 Not Found') runs

Short answer

A status line sent with header() only works before any output, and a later call can replace it. Check the status the browser really received. If it is 200, find the output or the later call that changed it. Use http_response_code(404) before output, then print your not-found page and stop.

  • Applies to PHP 8.3, Apache or nginx with PHP-FPM
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the status line the server actually sends, not at what the page looks like.

    curl -i https://example.com/missing-page | head -n 5
  2. 2Match what you see, then jump to its fix

    1. 1A warning about headers already sent, or no warning at all when display_errors is offOutput was already sent before header()Fix
    2. 2The code calls header('', true, 404) or similarThe $response_code argument is used with an empty headerFix
    3. 3The status reads 200 only for some paths, or after an include runsLater code sets another status and replaces the 404Fix
    4. 4http_response_code() returns false in a script run from the terminalThe status function is used in the wrong placeFix

What does the error look like?

  • The browser network panel shows 200 OK even though header('HTTP/1.0 404 Not Found') runs
  • The page content appears normally after the 404 header

Cause 1 of 4

Output was already sent before header()

1Check

header() must be called before any output: HTML tags, blank lines in a file or output from PHP. A space or a blank line before the opening PHP tag is enough.

php -l page.php && head -c 200 page.php | od -c | head

2Fix

Move the status call to the top of the script, before any echo, include with output or markup, and remove stray whitespace before <?php.

<?php
http_response_code(404);
// page markup starts after this

3Verify

Request the URL and read the status line.

curl -I https://example.com/missing-page

Confirmed by php.net

Did this fix it?

Cause 2 of 4

The $response_code argument is used with an empty header

1Check

The manual says the response code argument only has an effect if the header argument is not empty.

grep -rn "header(" --include=*.php . | head

2Fix

Use http_response_code(404), or pass a real status line to header().

http_response_code(404);
// or
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');

3Verify

Check the status line again with curl.

curl -I https://example.com/missing-page

Confirmed by php.net

Did this fix it?

Cause 3 of 4

Later code sets another status and replaces the 404

1Check

The first status line is always the first thing sent, and calling header() again with a new status line overrides it as long as headers are not yet sent. A framework bootstrap, a template or an included file can do that.

grep -rn "http_response_code\|HTTP/1" --include=*.php . | head -20

2Fix

Set the 404 as the last status call before output, then stop the script so nothing runs after it.

http_response_code(404);
include __DIR__ . '/404.html';
exit;

3Verify

Request a missing page and confirm 404 with the right body.

curl -i https://example.com/missing-page | head -n 12

Confirmed by php.net

Did this fix it?

Cause 4 of 4

The status function is used in the wrong place

1Check

In a web server environment it gets or sets the code. Outside a web server (the CLI) it behaves differently and returns false when you do not pass a code.

php -r 'var_dump(http_response_code());'

2Fix

Test status codes through the web server (curl), not by running the file in the terminal.

curl -i http://localhost:8080/page.php | head -n 3

3Verify

The first line of the response should be HTTP/1.1 404 Not Found.

curl -I http://localhost:8080/page.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Send status codes at the very top of the script, before any template or include.
  • Turn on error logging so a headers-already-sent warning is visible.
  • Test statuses with curl -I instead of judging by what the page looks like.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter works locally but every page is 404 on the live server

The requested URL was not found on this server.

A 404 on the live server comes from one of four places: the document root is not the public folder, Apache ignores .htaccess or nginx has no try_files rule, the route is not defined because auto routing is off, or the controller file name differs from its class in case.

5 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.