homepage Welcome to WebmasterWorld Guest from 54.205.197.66
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Page Header Status
Naveenn




msg:4578558
 12:06 pm on May 28, 2013 (gmt 0)

I have a very old php script (forum) running which I need to fix in a way to return the correct Apache status. The script returns a 302 status for all 404 pages. I would like to return a 404 for all pages which no longer exist.

This is a problem especially with the Google Webmaster tools showing all these as soft 404s errors.

 

g1smd




msg:4578578
 12:44 pm on May 28, 2013 (gmt 0)

Use the HEADER directive. This must be sent before any other output to the browser. The extra code will need to be very near the beginning of your PHP script.

Readie




msg:4580012
 8:46 pm on May 31, 2013 (gmt 0)

In the past when looking up the 404 header (I can't remember the syntax for everything :) ) I've seen people advise the following:

header('HTTP/1.0 404 Not Found', true, 404);
header('Status: 404 Not Found');

I'm not sure if having them both really makes any difference though.

swa66




msg:4580014
 8:51 pm on May 31, 2013 (gmt 0)

I think
header("HTTP/1.0 404 Not Found");
is enough.
More info: [php.net...]

lucy24




msg:4580024
 9:46 pm on May 31, 2013 (gmt 0)

I think it depends on your php version. Even if you're on shared hosting, they should say somewhere. There's one form that goes

:: shuffling papers ::

if (function_exists('http_response_code'))
{ http_response_code(404); }
else
{ header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found"); }
include ($_SERVER['DOCUMENT_ROOT'] . "/boilerplate/missing.html");


The last line is cut-and-paste from my own vaguely analogous code. This part is crucial-- the task, that is, not the exact filename-- because the 404 doesn't come from the server, so the 404 page won't be shown unless you explicitly code for it. If you leave it out, humans will see a perfectly blank screen. This is unnerving when it happens ;)

coopster




msg:4581274
 12:22 pm on Jun 5, 2013 (gmt 0)

I would like to return a 404 for all pages which no longer exist


You may also consider a 410 Gone. In the Apache conf you merely use:
Redirect gone /path/to/no-longer-exists.htm

More information:
[w3.org...]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
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