Forum Moderators: phranque

Message Too Old, No Replies

404 for Dynamic Pages

404 Question.

         

Dayo_UK

1:10 pm on Aug 26, 2004 (gmt 0)



With a dynamic site running from a database using mod rewrite how do you go about creating a 404 page if the database is empty.

eg if we have a page like this

www.mydomain.com/page/1.htm running from a database where data is displayed matching the variable 1 all is fine - but if someone changes the 1 to 1000 and there is no matching data in the database the page will be displayed but empty of data - ideally I want this to become a 404 for example just as it does at WebmasterWorld:-

www.webmasterworld.com/forum1/ - data pulled from database

www.webmasterworld.com/forum1000/ - database must be queried to see if forum exists and then 404 served as no matching data.

Dayo_UK

1:50 pm on Aug 26, 2004 (gmt 0)



I think I have sussed it.

I have gone for a PHP Header Redirect by first checking to see if data is returned based on the typed in url and then if nothing returned gone for a PHP Header Redirect to the 404.

jdMorgan

3:32 pm on Aug 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't redirect to a 404 page - you will confuse the search engines badly if you give the a 301 or 302 redirect to your 404 page. They will consider the URL good if they get a 301 or 302.

Instead, use the php header function to return a 404 status code, and then directly output your 404 error page contents from that same script.

Jim

Dayo_UK

3:46 pm on Aug 26, 2004 (gmt 0)



Hi Jim

The code I was using is as follows:-

<?php if($data== ""){
header("HTTP/1.0 404 Not Found");
header("Location: file/404error.htm");
exit;
}
?>

Does this look OK?

jdMorgan

3:54 pm on Aug 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, Yes that's fine, but you might consider adding the standard "The resource you requested was not found on this server." message for the sake of type-in users, otherwise, all they'll see is a blank page.

Jim

Dayo_UK

3:57 pm on Aug 26, 2004 (gmt 0)



Dont they see my custom 404 error page in the location file/404error.htm? This is what happens when I type in the urls.

Thanks for your help.

Dayo_UK

4:08 pm on Aug 26, 2004 (gmt 0)



Looking at my logs, I am seeing a 302 status returned to Googlebot and MSNbot who are both looking at these pages. Is this something to worry about? - My code is exactly as above. :?

jdMorgan

4:19 pm on Aug 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Apparently, you can't use the "header-location" function -- It will result in a 302 redirect, which will override the 404 response status you tried to output in the previous line. As I hinted at above, use the response function only, and then either hard-code the 404 page contents into your script or #include it into the current response. My language is bound to be a bit loose - I am not a php programmer.

Returning a 302 response to a 'bot in response to a request for a page that does not exist can be a disaster. First, you'll end up with duplicate content -- many copies of the 404 page itself, and second, your server will appear to be an "infinite URL space" to the 'bots, since no matter what URL they ask for, they'll get a 302 redirect to a page that exists (the 404 page). In this case, several 'bots are known to set a strict limit on how deep they'll spider your site -- They do not want to waste time on sites that will generate a page for any query, because it is a waste of their spidering resources and bloats their index with likely-to-be-useless auto-generated pages.

I'd consider getting this problem fixed to be a very high priority.

Jim

Dayo_UK

4:27 pm on Aug 26, 2004 (gmt 0)



Thanks Jim

I have removed the redirects for now :)

So if I use the following code:-

<?php if($data== ""){
header("HTTP/1.0 404 Not Found");
exit;
}
?>

The robots will see a 404 page? - the user will still go to the database page with no database content but at least the robots will see a 404 and I can hand code the database page for empty content?

jdMorgan

10:30 pm on Aug 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The robots will see a 404 page? - the user will still go to the database page with no database content but at least the robots will see a 404 and I can hand code the database page for empty content?

No, the robots. will see a 404 server response code. I cannot answer what your script will do as far as providing the content-body of the response -- that is up to your script. As far as the robots are concerned, a 404 response code is all they really care about, but in addition to that, you should return 'friendlier' content for your type-in users.

You may want to ask about this over in the PHP forum if you don't get more-informed responses here.

Jim