Link to home
Start Free TrialLog in
Avatar of Fernanditos
Fernanditos

asked on

How to Generate a 404 error and then redirect to 404.php?

Hi,

I need to generate a 404 error and then redirect to 404.php inside a condition:

example:

if $type = 'none' {
header("HTTP/1.0 404 Not Found");
...
}

How whould be the right way to condition this and after genetating a 404 error redirect to 404.php ?

Thank you for any help
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

You can set the name of the 404 handler in .htaccess.

You can issue the header ("Location") right after the "http/1.0 404..." header.
Here is the line from my .htaccess:

ErrorDocument 404 /404handler.php

So the entire setup might look like the code snippet...  But I am wondering if this will produce a very good user experience?  The meaning of 404 is pretty clear, and it's not usually the result of a logic decision inside a working script.
if $type = 'none' {
    header("HTTP/1.0 404 Not Found");
    header("Location: 404.php");
    exit;
}

Open in new window

Avatar of Fernanditos
Fernanditos

ASKER

Thank you Ray. I attach my real code bellow.

The condition is working and I the the echo printed out but the "header" are not working, I am not being redirected and I think it not returning the 404 error because I added a 404 handler in .htaccess and it is not having any effect.

Why header not working ?
if($this->type == 'none') {
                header("HTTP/1.0 404 Not Found");
		header("Status: 404 Not Found");
                header("Location: 404.php");
                echo 'Page not fount, Type:  '.$this->type;
                exit;
        }

Open in new window

What I get is a blank page with this:

Page not found, Type: none.

just my echo output.
Having the echo after the header("Location") does not make any sense to me.  But let me set up a test and get back to you.
While I am doing that, please have a look at this:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The echo statement should have no effect after the header("Location: xxx") redirect.  This code works fine on my system, and doesn't echo the Page not found dialog.

I figure that somewhere else in your code you already started echoing some output to the browser, and you can't send any header() calls after you've done that.  It should say that in your error_log though, try turning on debug in your script temporarily:

ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting(E_ALL | E_STRICT);

If you have not sent any output to the browser, your redirect should work perfectly with your 3 header lines.
@xterm: That is a good point.  Here is one of the ways to produce browser output without knowing that you did it.

<?php $x = "index.php"; ?>
<?php header("Location: $x"); // FAILS BECAUSE OF AN INVISIBLE END-OF-LINE CHARACTER AFTER THE CLOSE PHP TAG ABOVE
Thank you, thank you!

I made the tests and I found that all works fine after I removed this line:
header("Status: 404 Not Found");

That was like breaking all.

Ok, now my code is working:
header("HTTP/1.0 404 Not Found");
header("Location: 404.php");

But, how can I prove or make sure that this is returning a 404 error to Google ? I thought this header("Status: 404 Not Found"); would return a browser message "Page not found"

Anyway to make sure a 404 is returning ?
Look at your httpd access_log for that page name, and you'll see the code it sent:

your.server.com - - [01/Nov/2011:10:07:48 -0500] "GET /favicon.ico HTTP/1.1" 404 283 "-" "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1"

The code field is immediately after the "GET", so you can see what code your web server sent to the robot.
You might also find something in the headers if you use Firebug.  Not sure how that might work, but it seems intuitively possible.  Also, there is this:
https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/
Well, the live-http-headers plug in idea did not help.  Look at the logs.  I am certain that the script is sending a 404, but it may be sending a 200 OK before that.

Why are you doing this?  It's an "interesting" design to say the least!