The Core

Why We Are Here => Web Development => Topic started by: ukgimp on July 27, 2011, 03:09:18 PM

Title: PHP Redirect and 404 Error
Post by: ukgimp on July 27, 2011, 03:09:18 PM
Any ideas why the following wont work:

Quoteif(mysql_num_rows($result)==0){
   header('HTTP/1.1 404 Not Found');
   header('Location: /404.php');
    die();

     }

If I comment out both line in turn it eitgher returns a 404 or does the rediect with a 200.

I want the 404 plus redirect.

I have this in my htaccess also:

ErrorDocument 404 /404.php

Any ideas, php headers are doing my head in
Title: Re: PHP Redirect and 404 Error
Post by: Rooftop on July 27, 2011, 03:18:10 PM
I think you shouldn't redirect if it is a 404, as you are already saying that it isn't found.  redirecting would then return a new response (found we hope), which is where it is getting confused.  More usual would be to return the 404 error then include the contents of your 404 output, so something more like:

if(mysql_num_rows($result)==0){
  header('HTTP/1.1 404 Not Found');
  include('my404pagecontent.php');

    } else {
 // do all your usual output
}

Title: Re: PHP Redirect and 404 Error
Post by: Rooftop on July 27, 2011, 03:18:38 PM
ah - too slow: What Jason said.
Title: Re: PHP Redirect and 404 Error
Post by: ukgimp on July 27, 2011, 03:22:02 PM
>>changing it to include /404.php instead

How, lol. This driving me nuts
Title: Re: PHP Redirect and 404 Error
Post by: ukgimp on July 27, 2011, 03:29:47 PM
hurrah (after a little defining the path)

Thank you both.

Title: Re: PHP Redirect and 404 Error
Post by: I, Brian on August 23, 2011, 08:48:26 PM
If not a silly question, why not "HTTP/1.1 301 Moved Permanently" and redirect to homepage (or other preferred page) rather than give a 404?
Title: Re: PHP Redirect and 404 Error
Post by: Black Knight on August 31, 2011, 05:56:06 PM
If your site can't give a 404, it can't be trusted, because its a bottomless pit.  Googlebot especially is programmed to deliberately ask for broken URLs just to check a 404 can be gathered.  If it can't, it throttles the spidering to a level that seriously hurts.
Title: Re: PHP Redirect and 404 Error
Post by: ukgimp on August 31, 2011, 06:18:18 PM
I have a fear of doing that BK. That's why I always check for real 404s and only do case by case 301s