Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force 404 with PHP and .htaccess not working

I am attempting to throw my own 404 in PHP depending on certain GET vars. But the following is not working. I can confirm that the pge header is coming back with a '404 status code' though. .htaccess just doesn't seem to be redirecting correctly. Am I missing something?

PHP Code:

if(!$_GET['page']){
    header('HTTP/1.0 404 Not Found');
}

.htaccess Code:

ErrorDocument 404 /404.html

Many thanks!

like image 526
wilsonpage Avatar asked Aug 23 '11 16:08

wilsonpage


1 Answers

As far as Apache's concerned, it's done its job as it has properly found the page/script that the user's request called for. The fact that the script is outputting a 404 header is irrelevant to Apache, since its job was completed properly.

You'd need to have your PHP script output the 404 header, and include the page the Apache 404 handler points at it:

if (!$_GET['page']) {
    header('HTTP/1.0 404 Not Found');
    include('404.html');
    exit();
}

Don't do a redirect to the 404 page, as that'd just turn the hit into a normal GET request, with a 200 OK result.

like image 104
Marc B Avatar answered Oct 31 '22 15:10

Marc B