Forum Moderators: coopster

Message Too Old, No Replies

404 header and die()

         

ocon

3:22 pm on Dec 29, 2011 (gmt 0)

10+ Year Member Top Contributors Of The Month



My site uses mod rewrite to turn page requests like /item/something/ into /item.php?name=something. Then the php page looks up the name in my database and creates a page based on the database contents.

People however someone might go to a page that does not correlate to an entry in my database, something like /item/not_something/. I want to return a 404 error header if the page does not match.

Right now I have:

if($name... not in database){
header("HTTP/1.0 404 Not Found");
die();
}

I've tested this out, it seems to give the right header and it stops the page from loading but I don't like how it's handled in the web browsers.

In Chrome, I get a gray "Oops! This link appears to be broken." page whereas in the same browser if I type in an address that does not exist (such as /not/anything/) I get a white page that says "Not Found The requested URL /not/anything/ was not found on this server."

Firefox gives me to same type of results.

I was hoping somebody would be able to help me get my item.php page to give the "Not Found" error instead of the "Oops! This link appears to be broken." error.

I do not have a custom 404 implemented on my server.

rainborick

3:33 pm on Dec 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



After your call to header(), you need to echo() the contents of an HTML page. If you haven't done so already, this would be a good time to create a custom 404 page to give users an opportunity to recover from Page Not Found errors on your site. A simple page offering an explanation of the situation with some links to appropriate pages on your site is a good start. Google's got a nifty piece of JavaScript for 404 pages that will offer suggestions based on its knowledge of your site. You might want to try that. Good luck!

g1smd

3:37 pm on Dec 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The 404 HTTP header tells bots and searchengines there's no page here.

Now you need to send a page of HTML tags and human readable content telling the user what happened.

Make sure you do all your calls to the database and manipulation of data before sending the opening DOCTYPE of any HTML page. This way you get to change the preceding HTTP headers if you need to.

ocon

4:43 pm on Dec 30, 2011 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you. I plan on creating a custom 404 page soon, but I wanted to get these two pages the same before proceeding further. It's just bothering me that both are seen as 404 pages, but the different browsers were showing the two pages them differently. In neither case am I printing/echoing out any text. I just seems like there is some kind of fundamental flaw that I wanted to fix before worrying about a 404 error page.

g1smd

8:16 pm on Dec 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Internet Explorer used to show its own internal error message page if the one sent by the site was less than a certain size. No idea if any other browsers do anything like this.

coopster

6:11 pm on Jan 2, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Google Chrome may. In IE it was the "friendly page" feature prior to IE7. Here are a couple resources:

[support.microsoft.com...]
[en.wikipedia.org...]