Forum Moderators: coopster

Message Too Old, No Replies

Forcing a 404 page in PHP question

         

slobizman

3:09 pm on Nov 21, 2003 (gmt 0)

10+ Year Member



I have PHP-based discussion forum software, which when someone requests a now deleted topic or post, it displays a message saying the page is no longer available. I'd like to change this to force a 404 page for a variety of reasons--one of which is to tell search engines to take the page off their indexes.

I tried the following code on a test page:

header("HTTP/1.0 404 Not Found");

This works. However, there is the snag. I have a custom 404 error page which is set up in htacess. I thought that the htacess line "ErrorDocument 404 /error404page.php" would take effect here and display the custom 404 page, but apparently, the "header" statement bypasses that and insists on displaying the "real" 404 page.

Is there a way I can force it to execute the ErrorDocument function so that the server returns a 404, yet displays my custom 404 error page. (I don't want to just display the custom page without issuing a 404, or else the Search Engines will not understand the link is truly broken.)

davidpbrown

3:51 pm on Nov 21, 2003 (gmt 0)

10+ Year Member



Maybe try .htaccess with

ErrorDocument 404 error404page.php

ie. not /error404page.php

That said, deleted pages are better served by 410 Gone.

dpb

slobizman

4:31 pm on Nov 21, 2003 (gmt 0)

10+ Year Member



I've already got that in the htaccess file.

davidpbrown

5:15 pm on Nov 21, 2003 (gmt 0)

10+ Year Member



I only use PHP headers for 301's on occasion, maybe it demands you always use Location.

->
header("HTTP/1.1 410 Gone");
header("Location:http://www.xyz.com/error410page.php");

jamesa

7:27 pm on Nov 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you're sending a 404 header in your script you're "bypassing" the Apache handling of 404 pages. So basically you either need to output your custom 404 page manually in the script, or do a redirect like davidpbrown suggested.

thawebmaster

2:41 pm on Nov 22, 2003 (gmt 0)

10+ Year Member



You can use the

header("HTTP/1.0 404 Not Found");
exit;

code. But you have to keep in mind that this header only works when you haven't send any other data yet! I had nearly the same problem, but this solved it for me!

Greeetzzz ThA WebMasteR

jamesa

3:54 pm on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> or do a redirect

Actually come to think of it, the Location redirect may end up sending a 200 response on the following page (if it works at all). So I'd just output the HTML after you send the 404 header.

davidpbrown

8:31 pm on Nov 22, 2003 (gmt 0)

10+ Year Member



the Location redirect may end up sending a 200 response on the following page

It doesn't for 301, 302's so it should work for 404's as well.

dpb

davidpbrown

10:06 am on Nov 23, 2003 (gmt 0)

10+ Year Member



Sorry, jamesa is right of course. I wasn't thinking straight..
I just did a test and got a 200 response.