Skip to main content

How to display Apache"s default 404 page in PHP


I have a webapp that needs to process the URI to find if a page exists in a database. I have no problem directing the URI to the app with .htaccess:




Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [NC]



My problem is that if the page does not exist, I do not want to use a custom 404 handler written in PHP, I would like do show the default Apache 404 page. Is there any way to get PHP to hand execution back to Apache when it has determined that the page does not exist?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. The only possible way I am aware of for the above scenario is to have this type of php code in your index.php:

    <?php
    if (pageNotInDatabase) {
    header('Location: ' . $_SERVER["REQUEST_URI"] . '?notFound=1');
    exit;
    }


    And then slightly modify your .htaccess like this:

    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{QUERY_STRING} !notFound=1 [NC]
    RewriteRule ^(.*)$ index.php?p=$1 [NC,L,QSA]


    That way Apache will show default 404 page for this special case because of extra query parameter ?notFound=1 added from php code and with the negative check for the same in .htaccess page it will not be forwarded to index.php next time.

    PS: A URI like /foo, if not found in database will become /foo?notFound=1 in the browser.

    ReplyDelete
  2. I don't think you can "hand it back" to Apache, but you can send the appropriate HTTP header and then explicitly include your 404 file like this:

    if( !$exists ) {
    header("HTTP/1.0 404 Not Found");
    include_once("404.php");
    exit;
    }

    ReplyDelete
  3. Call this function:

    http_send_status(404);

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.