header() for a 404 does nothing in PHP: the page still shows 200
The browser network panel shows 200 OK even though header('HTTP/1.0 404 Not Found') runsShort 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.
Fix it fast
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 52Match what you see, then jump to its fix
- 1
A warning about headers already sent, or no warning at all when display_errors is offOutput was already sent before header()Fix - 2
The code calls header('', true, 404) or similarThe $response_code argument is used with an empty headerFix - 3
The status reads 200 only for some paths, or after an include runsLater code sets another status and replaces the 404Fix - 4
http_response_code() returns false in a script run from the terminalThe status function is used in the wrong placeFix
- 1
What does the error look like?
The browser network panel shows 200 OK even though header('HTTP/1.0 404 Not Found') runsThe 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 | head2Fix
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 this3Verify
Request the URL and read the status line.
curl -I https://example.com/missing-pageConfirmed by php.net
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 . | head2Fix
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-pageConfirmed by php.net
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 -202Fix
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 12Confirmed by php.net
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 33Verify
The first line of the response should be HTTP/1.1 404 Not Found.
curl -I http://localhost:8080/page.phpConfirmed by php.net
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Back end
Warning: Cannot modify header information - headers already sent
Warning: session_start(): Session cannot be started after headers have already been sent
PHP sent output to the browser before your header(), setcookie() or session_start() call, and headers must go first.
3 causes, each with a fixBack end
Chrome shows net::ERR_INCOMPLETE_CHUNKED_ENCODING on a PHP page
The page loads partly, then the response stops
The browser got a chunked response that ended before its final zero-length chunk, so the connection closed mid-page.
2 causes, each with a fixBack 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 fixBack end
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixBack end
Laravel routes return 404 or throw NotFoundHttpException
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
A 404 means Laravel found no route for that URL, or the request never reached Laravel at all.
3 causes, each with a fixBack end
Livewire uploads work locally but fail behind nginx on the server
$_POST and $_FILES are empty (PHP discards a body over post_max_size)
Three separate limits stop a large upload, and a local dev server hides two of them.
3 causes, each with a fix