homepage Welcome to WebmasterWorld Guest from 54.234.0.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / WebmasterWorld / Webmaster General
Forum Library, Charter, Moderators: phranque

Webmaster General Forum

    
Resending HTTP status code late in page generation, is it ok?
Luxoria




msg:4603021
 4:41 pm on Aug 18, 2013 (gmt 0)

I am working with a PHP shopping cart and using a mod that rewrites the URL. The issue is when a product is removed and the URL is called, the code generates a soft 404 page stating the product is not found, yet the HTTP status code is 200.

As a fix I added
header('HTTP/1.1 404 Not Found'); late in the page generation where the soft 404 text is generated (assuming after the original 200 code was send)

When I added this code and did a 'Fetch as Google' Google kicks back 'Not Found' which is what I want for products no longer in the store.

My question: Is sending a HTTP header status late in page generation after a 200 was sent OK to do?

thanks

 

lucy24




msg:4603109
 12:14 am on Aug 19, 2013 (gmt 0)

Short answer: If g### reports a 404, then the header is being sent successfully.

Slightly longer answer: Most of the time, the header your server sends is the header the user receives. What you have here is the exceptional case where they are different.

The server's task is to find the php page responsible for filling the request. Once the page is found, the server reports a 200 and its work is done. It is now up to the php page to look at the details of the request and decide which response is appropriate. If the php is correctly written-- as yours apparently is-- this second response is the one the user sees.

Now, can you train yourself not to say "soft 404"? This phrase is widely used by google to mean a specific thing: a request that should receive a 404, but instead receives some other response such as a 301 redirect to the home page.

Is sending a HTTP header status late in page generation after a 200 was sent OK to do?

No, but this doesn't seem to be what you're doing, so you are OK. What's not OK is to try to send out a non-200 header after part of the page has already been sent out, not simply built. Then it's too late. A response header is, by definition, sent out together with the page. As long as you've got some kind of output buffering in place, you're fine.

Disclaimer: I only know about three words of php. The 404 header thing happens to be one of them.

g1smd




msg:4603199
 8:37 am on Aug 19, 2013 (gmt 0)

The HTTP Header must be sent out before you send any of the HTML code and page content.

Think about it this way, when you send an email, there is an email header that says who the message is to and what the subject is.

If you were to type Cc: Jon Doe at the bottom of the email, that person would not receive a copy of the email. The header information must be sent before the text of the email.

The same applies to HTML pages. The HTTP Header must be complete before the HTML part of the page is sent.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / WebmasterWorld / Webmaster General
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved