404 not found page not working with my .htaccess

404 not found page not working with my .htaccess

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

Stefan Bogdanescu

Founder & Senior Architect

Why Your ErrorDocument 404 Rule Is Being Ignored by Apache

You have configured your .htaccess file with a specific intent: serve a custom page whenever a user requests a resource that doesn't exist. However, if you are looking at your server logs or browser behavior and seeing the default system error page instead of your custom main/404_not_found.html, the culprit is almost certainly not the syntax of your ErrorDocument directive itself. The issue lies in how Apache evaluates directives relative to one another and the strict ordering required within your configuration file.

The snippet you provided contains a fundamental logical flaw regarding execution order. You have placed the RewriteRule directive immediately before the ErrorDocument declaration. In Apache's request-processing lifecycle, rewrite rules are evaluated during the "rewrite phase," which occurs early in the pipeline. If a rule matches a request and performs an internal redirect or a sub-request without issuing a final response code (like a 301 or 200), the server continues to process the request based on the rewritten URI.

The specific problem arises because your rewrite rule attempts to match article_detail/.../...\.html. If this pattern matches the URL you are requesting, Apache treats it as a successful match and attempts to execute the internal redirect to article_detail.php. Even if that PHP file subsequently fails or returns a 404 status code, the server has already committed to handling that specific path via rewrite logic. More critically, in many configurations, if a RewriteRule matches and does not explicitly stop further processing (via [L]), subsequent directives in the same .htaccess file may be bypassed depending on the Apache version and AllowOverride settings. However, the more common scenario with your specific setup is that the rewrite engine is "on," and if a rule matches, it effectively consumes the request logic before the generic error handler can catch a missing file at the root level, assuming the rewritten target also fails to resolve cleanly or if the browser interprets the redirect loop.

But there is an even more direct reason this ErrorDocument directive appears to fail: directive priority and scope. The ErrorDocument 404 directive must be placed before any rules that might inadvertently match the request and return their own response, or it must be scoped correctly. In your code, you are relying on a catch-all behavior. If your RewriteRule matches, Apache redirects the user to article_detail.php. If article_detail.php does not exist (or returns a 404), Apache generates a new error page for that new path. The original ErrorDocument rule you defined likely applied to the original URL, but the internal redirect changed the context entirely.

To fix this, you must ensure that your custom error page is served regardless of whether the request hits a rewrite rule first. The most robust solution is to define the ErrorDocument 404 directive at the top of your .htaccess file, before any RewriteEngine or RewriteRule directives. This ensures Apache knows exactly which URL path to serve when encountering a 404 status code from any source (whether it's a missing file, a failed rewrite target, or a non-existent directory).

ErrorDocument 404 /main/404_not_found.html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article_detail/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ article_detail.php?article_det=$2 [L]

In this corrected structure, the ErrorDocument is absolute (using a leading /) and placed first. This guarantees that if Apache ever generates a 404 status—whether for a missing static asset or a failed rewrite target—it immediately serves your custom HTML file. Note the addition of [L] (Last) to the rewrite rule; this stops further rewriting once the article detail logic is handled, preventing potential conflicts with error handlers.

Additionally, ensure that Options +FollowSymLinks does not inadvertently interfere if you are expecting static files to be served directly. If your custom 404 page resides in a directory like /main/, verify that the web server user (often www-data or apache) has read and execute permissions on that folder. A common pitfall is configuring the error document relative to the current directory without accounting for where the file actually sits in the filesystem hierarchy.

If you are moving away from Apache's .htaccess limitations to gain better control over request handling, consider migrating your routing logic directly into your application code. Frameworks like Laravel handle 404 pages natively through their exception rendering system (App\Exceptions\Handler). This approach is often cleaner than mixing server-level rewrite logic with global error handlers, as it allows you to define the response content dynamically based on the route group or controller state. For more complex routing scenarios where you need fine-grained control over how missing resources are presented, leveraging a modern framework's built-in exception handling provides a more maintainable architecture than relying solely on server-side directives.

Our network