homepage Welcome to WebmasterWorld Guest from 54.196.196.108
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

    
Proper 404 handling?
How to redirect to a custom 404 from a partial index.php
MaxGrenk




msg:4304683
 8:21 am on Apr 27, 2011 (gmt 0)

I have a custom 404.php
It works nicely, if the file or directory does not exist; e.g. it sends a proper 404 in the header.

My query is: How can I achieve the same clean result when partially running the index.php?

My page calls are like this: www.sub.domain.net/ for the index.php or www.sub.domain.net/?topic=3 for the same index.php but now with the querystring containing the page id to be served.

The pages does a database query and figures there is not page with the id=3. It is this case that I want to hand over to the 404.php page to send the 404 and display any options for the user.

When do the redirects in the index.php file (see below), I always get a 302 on the /?topic=3 and then a 200 on the 404.php.

It almost looks to me me that the behaviour might be correct, but it is not what I want. What I would like is the 404.php to show with a 404 status response header.

Any ideas how I can fix this? If at all :o)

I am using this code:
header('HTTP/1.0 404 Not Found');
header('Status: 404 Not Found');
header('Location: /err/404.php');

I figured I must include line 1 to get a 404 in the server response; line 2 makes no difference.
When I add line 3 I get 302 > 200

I am also using ob_start(); at the beginning of index.php, which allows header changes until final output.

 

g1smd




msg:4304695
 9:24 am on Apr 27, 2011 (gmt 0)

By specifying a location, i.e. a URL, the server will send a redirect response, and the browser will make a new request for that new URL.

The redirect response is a 302 unless you specify that it should be a 301.

When the second request is issued, the file will be returned with "200" status because the file you requested DOES exist.

The correct approach is for you to "include" the HTML for the error file and send it out after the header. The "include" does not make the browser request a different URL. Instead the file is sent to the browser as a direct reply to the current request.

ErrorDocuments should never be a redirect. The response code should be returned at the originally requested URL.

MaxGrenk




msg:4304719
 10:18 am on Apr 27, 2011 (gmt 0)

Thanks for the response.
I had to read it multiple times before it sunk in ... my problem, as I am not an experienced programmer :)

So, you are saying the 302 > 200 is by design, which I suspected to be the case.

But, rather than redirecting to the err/404.php I should include the HTML for that page, rather than redirecting to it.

This means I code as follows:
header('HTTP/1.0 404 Not Found');
header('Status: 404 Not Found');
Display404Html();
exit;

I could even pass in the query string an some logic to say the page with the id=3 does not exist. Please use search blah blah...

Great... just tested this: it works -- thanks!

One more question: why is the standard response a 302 and can I make it a 301 (since I never specified to reply as 302 in the first place)?

This question is of importance to me, as I am trying to solve another problem, where Google complains in WebMasterTools (sitemap) about 302 redirects on all my pages, and I can't locate the root cause.
As a result 720 of my 800 pages have dropped off Google's index.
I can post anew thread for that?!

g1smd




msg:4304722
 10:31 am on Apr 27, 2011 (gmt 0)

In .htaccess, the
[R] flag produces a 302 redirect. You must specify [R=301] if that is what you actually want. You must also add the [L] flag, so [R=301,L] is correct.

In PHP,
header('Location: http://www.example.com/somepath'); produces a 302 redirect.

If you want a 301 redirect, you must add this line first:
header('HTTP/1.1 301 Moved Permanently');

Note that for any redirect (either in .htaccess or in PHP) you MUST include the protocol and domain name too. This is a response back to the browser telling it to fetch a different URL.

However, in the case of missing pages for which you will show a standard error message, you do not want to redirect at all. You return the 404 status for the currently requested URL.

[edited by: g1smd at 10:46 am (utc) on Apr 27, 2011]

MaxGrenk




msg:4304728
 10:41 am on Apr 27, 2011 (gmt 0)

Got it! Thanks!

And... silly me (I am currently rewriting ASP > PHP) forgot to say that the 302 problem exists on the current ASP site... maybe I should use a proper web environment by moving to PHP shortly :) as in sooner than planned!

g1smd




msg:4304729
 10:47 am on Apr 27, 2011 (gmt 0)

You can do this stuff in IIS and in ASP but it is a lot more fiddly, and quite error-prone, than the relatively straightforward methods used in Apache and PHP.

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