Grow your CSS skills. Land your dream job.

Apache ErrorDocument 404 but without the automatic 404 header?

  • # April 4, 2012 at 7:21 am

    I’m trying to get my 404 document to check the DB to generate a page if the URL provided is listed there, but the server seems to send out a 404 header before I get a chance (in the 404 script) to send a 200 (if there is a matching page reference in the database – how can I prevent this?

    # April 4, 2012 at 7:31 am

    My 404 psuedocode:


    1. get URL
    2. Check URL in Database
    3. If it exists:
    Send 200 header and output DB-driven HTML
    If not:
    Send 404 header and output error HTML
    # April 4, 2012 at 7:31 am

    Problem is that Apache sends the 404 header BEFORE it runs my 404 script.

    # April 4, 2012 at 8:04 am

    I assume you have a custom 404 page setup that all ‘not found’ pages route to?

    Surely then, in this page, all you need to do is search for the initially requested URL and, if found, redirect the user to the correct page?

    # April 4, 2012 at 8:07 am

    OK, but the idea here is that my pages are entirely database driven and don’t have actual files in the webspace. Do you see what I mean?

    # April 4, 2012 at 10:39 am

    Yes, that’s essentially the way I build my sites.

    Everything is routed through the index.php or default.php, correct?

    I recommend creating a static 404 page rather having than a database driven one. Then, when you send the 404 header it’ll just show the custom error page. You just need to edit your httpd.conf file to set the new 404 page

    # April 4, 2012 at 10:48 am

    I’m not sure I have acces to the httpd.conf as this project is on shared hosting, even the the.htaccess is locked down to a few ‘allowed’ options.

    If I change to a static 404 page, how will I get the server to execute the index.php (in the webroot?) that will generate the page if the client requested URI is http://www.examplesite.com/exampleproduct/index.php (which, of course, won’t actually exist?)

    # April 4, 2012 at 11:53 am

    Make an index.php file, then route all your pages through it. Your URL would look like this:

    http://example.com/index.php?page=page-title
    # April 4, 2012 at 4:10 pm

    Got it! (finally)

    In my .htaccess file, instead of using:

    ErrorDocument 404 /error/

    I used:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . /error/

    to execute the error script in the event a requested file or folder could not be found. The benefit of this being that no header is sent and you can then decide in that script which header you want to send and what to display (based on page info you might have in the DB etc.)

    # April 5, 2012 at 2:38 pm

    So that could potentially force all error traffic to go to a CMS created page?

    # April 5, 2012 at 2:39 pm

    If the page URL doesn’t exist in the DB, then the script responds with a 404 header and custom 404 HTML (not CMS driven).

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".