PHP Redirect and 404 Error

Started by ukgimp, July 27, 2011, 03:09:18 PM

Previous topic - Next topic

ukgimp

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

Rooftop

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
}


Rooftop

ah - too slow: What Jason said.

ukgimp

>>changing it to include /404.php instead

How, lol. This driving me nuts

ukgimp

hurrah (after a little defining the path)

Thank you both.


I, Brian

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?

Black Knight

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.

ukgimp

I have a fear of doing that BK. That's why I always check for real 404s and only do case by case 301s