wilsonpage
wilsonpage

Reputation: 17610

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!

Upvotes: 11

Views: 5754

Answers (2)

Eric Sebasta
Eric Sebasta

Reputation: 224

You cannot make the error page start with a number on a XAMPP test server I know...

It may be true for windows ... period.

that one eluded me for hours!

Try renaming your error page to:

error404.html

then change your .htaccess Error section for your 404 to:

ErrorDocument 404 /error404.html

And don't forget to start it with the forward slash. That will fix it.

Upvotes: 0

Marc B
Marc B

Reputation: 360562

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.

Upvotes: 13

Related Questions