15

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?

anubhava
  • 761,203
  • 64
  • 569
  • 643
Matt
  • 1,287
  • 2
  • 11
  • 25
  • Not really. You could do a simple redirect via `header('Location: ...')` to the 404 page, but that'd show up as a '200 OK' request, which is considered poor practice. – Marc B May 10 '11 at 16:17
  • 1
    this might help you: http://stackoverflow.com/questions/4232385/php-or-htaccess-make-dynamic-url-page-to-go-404-when-item-is-missing-in-db – Marco Demaio May 10 '11 at 16:19
  • I think there is still no easy option for this, http://stackoverflow.com/q/4856425/345031 – mario May 10 '11 at 16:42

3 Answers3

17

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;
}

Update

PHP 5.4 introduced the http_response_code function which makes this a little easier to remember.

if (! $exists) {
    http_response_code(404);
    include_once("404.php");
    exit;
}
andrewtweber
  • 24,520
  • 22
  • 88
  • 110
6

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This loops if you call this from a 404 handler. – Mel May 10 '11 at 17:07
  • `header('Location: /non-existent-page-url');` wasn't supposed to be from your custom 404 handler. See my answer, I wrote index.php above. In fact you shouldn't really have a custom 404 handler if you want to show Apache's 404 handler. I would suggest commenting out `ErrorDocument 404` in your apache config or .htaccess first. – anubhava May 10 '11 at 17:15
  • This is how I did it: I used your suggestion in .htaccess plus the solution that Marco Demaio suggested which gets the page /foo?notFound=1 with file_get_contents() and echoes it. Not exactly what I wanted but close enough. – Matt May 11 '11 at 11:16
  • @Matt; Glad that it worked. However I thought you are just trying to show Apache's **default 404 error page** and don't have any custom 404 page using `ErrorDocument 404`. If that's indeed the case you don't need any `file_get_contents()` type command. – anubhava May 11 '11 at 13:22
4

Call this function:

http_send_status(404);
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
AJ.
  • 27,586
  • 18
  • 84
  • 94