How to create an error 404 page using PHP?

How to create an error 404 page using PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing PHP 404 Errors in .htaccess Reroutes

When you offload routing logic to a single entry point like /page.php, you are essentially creating a "catch-all" route that intercepts requests for dynamic resources. In your specific setup, the .htaccess file maps any request under /word_here to page.php?name=word_here. Once inside the PHP script, you validate the requested name against an internal array of valid pages. If the name is missing from that list, the script must signal a 404 status code to the web server so that it can serve your custom error page defined in the ErrorDocument directive.

The core issue with attempting to solve this by manually setting the status header lies in how Apache processes headers and the HTTP protocol itself. You correctly identified that simply echoing content or redirecting via header("Location: ...") often fails to trigger the custom error handler if done incorrectly, but relying solely on a raw header string also has pitfalls.

Consider this line of code you tried:

header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found");

This approach is fundamentally flawed for two reasons. First, the header() function in PHP expects the first argument to be either a status code (e.g., 404) or a header string formatted as Header-Name: Value. It does not accept a full HTTP response line like HTTP/1.1 404 Not Found as its primary argument to set the status. Second, and more critically, you must ensure no other output has been sent before calling this function. If your script attempts to render any HTML or echo statements before hitting this header, Apache will abort the request with a "Headers already sent" error, preventing the status code from ever reaching the client.

The correct way to simulate a 404 in this architecture is straightforward but strict. You must send the status code immediately after determining that the resource is missing. Use the integer representation of the status code:

http_response_code(404);
header('Content-Type: text/html');

By calling http_response_code(404), you explicitly tell PHP to set the response status line to HTTP/1.1 404 Not Found. Following this, you should optionally send a Location header if you want to guide the user back to your home page or root URL, though this is not strictly required for the error page to load.

However, there is a significant architectural trade-off here. When you return a 404 status code from page.php, Apache reads that status and looks up the ErrorDocument directive in your .htaccess file. If configured correctly, Apache will then serve the HTML file you specified (e.g., error_404.html) instead of executing any further PHP logic in your script. This is why redirecting to your error page via a 301 or 302 is often discouraged in this specific context; a redirect changes the final URL visible in the browser and can alter SEO signals, whereas a direct 404 status with an ErrorDocument directive preserves the original request path while serving a custom response.

If you find that http_response_code() is not triggering your custom error page, check two common pitfalls:

  1. Output Buffering: Ensure no whitespace or content exists before the header call. Even a single newline character can break header manipulation.
  2. Order of Operations: If you are using frameworks or libraries that automatically start output buffering, verify they aren't interfering with the header transmission.

For developers moving away from procedural PHP or those looking for a more robust routing solution, platforms like Laravel handle these edge cases internally. In a modern framework context, you would typically define a route group that catches all requests and return a Response object with a 404 status within your controller logic, allowing the framework to manage the response rendering seamlessly. For static sites or simpler PHP setups, sticking to http_response_code(404) remains the most efficient method to leverage existing .htaccess configurations without duplicating content in every script.

Our network