iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-18-2009, 04:51 PM
caravan's Avatar
WebProWorld Pro
 
Join Date: May 2006
Location: Preston, Lancashire, UK
Posts: 102
caravan RepRank 1
Default 404 response not working

I'm having problems creating a 404 error page and returning a valid 404 response for the search engines using php.

When someone lands on a product details page which no longer exists I want to return a 404 response to the search engines and display an appropriate message to the visitor. I've tried several ways of going about this but I'm getting mixed results and problems.

When a visitor lands on a product detail page I first make a call to the database to ascertain whether the product exists or not. If not I've tried returning a 404 with the following php code:

Code:
<?php header("HTTP/1.0 404 Not Found"); ?>
What this is doing is returning a 200 response and then breaking the html page half way through so the screen is just displaying a page of html code (not rendering the html).

I've also tried redirecting the visitor to a custom 404 page by initially using a 301 redirect to the custom 404 page then serving a 404 response but again this returns a 200 response.

I found something which suggested that ErrorDocument 404 /errorpage.php should be added to the end of the htaccess file but this doesn't seem to do anything, probably because a 200 reponse is being returned I suppose.

All the searches I have done on this problem always seem to point the fact that the <?php header("HTTP/1.0 404 Not Found"); ?> code is correct and should be served before any html output has been done. Has anyone got any advice on how I should be handling this as I'm slowly tearing my hair out over it.
Reply With Quote
  #2 (permalink)  
Old 10-18-2009, 05:21 PM
caravan's Avatar
WebProWorld Pro
 
Join Date: May 2006
Location: Preston, Lancashire, UK
Posts: 102
caravan RepRank 1
Default Re: 404 response not working

As a follow up on this, I've just created a basic html page with just the following at the top of the page

Code:
<?php
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
?>
Running the url through an online http header check tool does not return anything but when I moved the file to another linux hosted website it worked. It also worked on a windows hosted website.

Could it be that the htaccess url rewriting that the site uses is affecting the 404 reponse?
Reply With Quote
  #3 (permalink)  
Old 10-18-2009, 07:50 PM
danlefree's Avatar
WebProWorld Pro
 
Join Date: Jun 2005
Location: Seattle
Posts: 268
danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4
Default Re: 404 response not working

The problem with your first attempt is that the code you are using to determine whether or not to return a 404 error is executing after your PHP parser has already sent output to the browser.

The problem with your second test very likely has something to do with your host's setup (they may be including some code or output which is being sent to the browser before your script runs).

You can contact your host about this - if they can't help you, I would recommend moving to the other host you tried (i.e. a host which is not doing anything "behind the scenes").
__________________
Dan LeFree | Product Manager (Linux VPS Hosting) | Owner/Operator (Web development, marketing)
Reply With Quote
  #4 (permalink)  
Old 10-19-2009, 05:44 PM
WebProWorld Member
 
Join Date: Oct 2005
Posts: 36
wdillsmith RepRank 0
Default Re: 404 response not working

I recently added a custom 404 on my site and it is working. I did it by adding a line to .htaccess like:

ErrorDocument 404 /errorpage.php

Try accessing a gibberish page, like www.yourdomain.com/pagedoesnotexist.html and see what happens. That will take your other php coding efforts out of the equation.

Once you are getting the right page returned, then you can use an http header check website to make sure your site is actually returning a 404 header.

I assume when you tried you created a .htaccess file (starts with a period)? Also, this may only work if you are running Apache web server.
__________________
Looking for a unique gift? Send one of our gourmet cookie bouquets today.
Food lovers - visit the Gourmet Gift of the Day Blog for delicious ideas.
Reply With Quote
  #5 (permalink)  
Old 10-19-2009, 10:02 PM
deepsand's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2004
Location: Philadelphia, PA
Posts: 3,226
deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9
Default Re: 404 response not working

Quote:
Originally Posted by caravan View Post
As a follow up on this, I've just created a basic html page with just the following at the top of the page

Code:
<?php
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
?>
Running the url through an online http header check tool does not return anything but when I moved the file to another linux hosted website it worked. It also worked on a windows hosted website.
Does the server in question support PHP?
Reply With Quote
  #6 (permalink)  
Old 10-20-2009, 10:06 AM
caravan's Avatar
WebProWorld Pro
 
Join Date: May 2006
Location: Preston, Lancashire, UK
Posts: 102
caravan RepRank 1
Default Re: 404 response not working

Quote:
Originally Posted by danlefree View Post
The problem with your second test very likely has something to do with your host's setup (they may be including some code or output which is being sent to the browser before your script runs).

You can contact your host about this - if they can't help you, I would recommend moving to the other host you tried (i.e. a host which is not doing anything "behind the scenes").
I'm in the process of contacting the host company to try to get to the bottom of this. I think the source of the problem is with the hosting company and their setup. If the issue isn't resolved with them I think I'll setup a test with another host and if successful move the site.

Quote:
Originally Posted by wdillsmith View Post
I recently added a custom 404 on my site and it is working. I did it by adding a line to .htaccess like:

ErrorDocument 404 /errorpage.php

Try accessing a gibberish page, like www.yourdomain.com/pagedoesnotexist.html and see what happens. That will take your other php coding efforts out of the equation.

Once you are getting the right page returned, then you can use an http header check website to make sure your site is actually returning a 404 header.

I assume when you tried you created a .htaccess file (starts with a period)? Also, this may only work if you are running Apache web server.
I've tried this but in this case I first need to determine if the database is returning any records. If not then the page would just load but with no information available. This is why I'm trying to trap these errors and return a 404.

As a short term measure I'm calling a 301 redirect to an appropriate related page.

Quote:
Originally Posted by deepsand View Post
Does the server in question support PHP?
It does support php as that is the core language used on the site.
Reply With Quote
  #7 (permalink)  
Old 10-20-2009, 10:34 AM
wige's Avatar
Moderator
WebProWorld Moderator
 
Join Date: Jun 2006
Location: United States
Posts: 2,648
wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9
Default Re: 404 response not working

Just because I didn't see this mentioned yet... is the header() call the absolute first thing you are outputting? There can not be so much as an empty line being sent to the browser before you send header information - the header call should really be contained in the top <?php ?> block, which should have no blank lines above the opening <?php tag.
__________________
The best way to learn anything, is to question everything.
Reply With Quote
  #8 (permalink)  
Old 10-20-2009, 05:34 PM
deepsand's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2004
Location: Philadelphia, PA
Posts: 3,226
deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9
Default Re: 404 response not working

Odd; I've have expected a decent pre-processor to ignore any such white-space.

Have you any idea as to whether such behavior is universal or platform dependent?
Reply With Quote
  #9 (permalink)  
Old 10-20-2009, 09:50 PM
danlefree's Avatar
WebProWorld Pro
 
Join Date: Jun 2005
Location: Seattle
Posts: 268
danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4danlefree RepRank 4
Default Re: 404 response not working

Quote:
Originally Posted by wige View Post
Just because I didn't see this mentioned yet...
I believe it was mentioned that, actually.


Quote:
Originally Posted by deepsand View Post
Odd; I've have expected a decent pre-processor to ignore any such white-space.

Have you any idea as to whether such behavior is universal or platform dependent?
It's universal to PHP - though use of the output buffer is a common workaround (particularly with legacy applications).
__________________
Dan LeFree | Product Manager (Linux VPS Hosting) | Owner/Operator (Web development, marketing)
Reply With Quote
  #10 (permalink)  
Old 10-21-2009, 01:15 PM
caravan's Avatar
WebProWorld Pro
 
Join Date: May 2006
Location: Preston, Lancashire, UK
Posts: 102
caravan RepRank 1
Default Re: 404 response not working

Quote:
Originally Posted by wige View Post
Just because I didn't see this mentioned yet... is the header() call the absolute first thing you are outputting? There can not be so much as an empty line being sent to the browser before you send header information - the header call should really be contained in the top <?php ?> block, which should have no blank lines above the opening <?php tag.
Looking back at my test file the first line calls the 404 with no white space. The 404 is called before the html code is output. This file is as basic as a html file can get but still doesn't return a valid 404. I've been busy this week but will do some further tests and tech support calls as soon as I can.
Reply With Quote
  #11 (permalink)  
Old 10-21-2009, 06:05 PM
deepsand's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2004
Location: Philadelphia, PA
Posts: 3,226
deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9deepsand RepRank 9
Default Re: 404 response not working

Have you tried using a server side script other than PHP?
Reply With Quote
  #12 (permalink)  
Old 10-31-2009, 12:30 PM
WebProWorld New Member
 
Join Date: Sep 2009
Posts: 13
freezea RepRank 0
Default Re: 404 response not working

If you are handling the 404 error pages in PHP yourself, you need to send out the correct HTTP response header and then DO not redirect the user with a "Location:" link. Most often, this is the case when these issues arise.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to buy a domain but no response Blitzburgh Domains Buy/Sell 7 06-08-2009 02:47 AM
Response.redirect realbeard Search Engine Optimization Forum 2 06-06-2008 11:56 AM
http://www.superactive.biz what 's working / not working jrobin747 Submit Your Site For Review 3 02-09-2005 12:16 AM
Get Response-Autoresponder ms_promoqueen Web Programming Discussion Forum 0 03-03-2004 09:51 AM


All times are GMT -4. The time now is 09:32 AM.



Search Engine Optimization by vBSEO 3.3.0