[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

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
0
Fernanditos
Asked:
Fernanditos
  • 8
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
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.
0
 
Ray PaseurCommented:
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

0
 
FernanditosAuthor Commented:
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

0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
FernanditosAuthor Commented:
What I get is a blank page with this:

Page not found, Type: none.

just my echo output.
0
 
Ray PaseurCommented:
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.
0
 
Ray PaseurCommented:
While I am doing that, please have a look at this:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
0
 
Ray PaseurCommented:
This script worked perfectly to redirect.
<?php // RAY_404_causer.php
error_reporting(E_ALL);

// COPY THE REQUEST ARGUMENTS
$q = (isset($_GET["q"])) ? '?q=' . $_GET["q"] : NULL;

// REDIRECT
header("HTTP/1.0 404 Not Found");
header("Location: RAY_404_handler.php" . $q);
exit;

Open in new window

and this script worked perfectly to handle the redirected request.
<?php // RAY_404_handler.php
error_reporting(E_ALL);
var_dump($_GET);

Open in new window

By carrying the request argument through the two scripts, I was able to see the request in the 404 handler script.  That is where you might want to issue any error messages, since the old page will disappear from the client browser as soon as the redirect happens.
0
 
xtermCommented:
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.
0
 
Ray PaseurCommented:
@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
0
 
FernanditosAuthor Commented:
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 ?
0
 
xtermCommented:
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.
0
 
Ray PaseurCommented:
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/
0
 
Ray PaseurCommented:
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!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now