Correct PHP for a 404 redirect?
I have to use custom 404 page for url redirection (.htaccess is not an option). I'm thinking of using the code below. I'm trying to account for valid redirection, and for the real case of a page not found, by adding a flag to the URL. What do you think?
<?php
// check GET for the flag - if set we've been here before so do not redirect
if (!isset($_GET['rd'])){
// send a 200 status message
header($_SERVER['SERVER_PROTOCOL'] . ' 200 Ok');
// redirect
$url = 'new-url/based-on/some-logic.php' . '?rd=1';
header('Location: ' . $url);
exit;
}
开发者_高级运维
// no redirection - display the 'not found' page...
?>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404:</h1>
<h3>Sorry, the page you have requested has not been found.</h3>
</body>
</html>
Edit - the reason .htaccess is not an option is because I'm running on IIS6 without apache.
Add 404 header generation:
...
header("HTTP/1.0 404 Not Found");
// for FastCGI: header("Status: 404 Not Found");
// no redirection - display the 'not found' page...
?>
...
And remove 200 code:
// delete this line
header($_SERVER['SERVER_PROTOCOL'] . ' 200 Ok');
// because code should be 302 for redirect
// and this code will be generated with "Location" header.
IF i am not getting you wrong, you want a Page to be shown for the 404 pages, that is, pages which do not exist anymore.
to the best of my knowledge (might be not too much) You cannot just set a php page as 404 handler without .htaccess or the apache conf file.
what i remember is that a 404.shtml is the default file for 404 handlers in normal apache setups, so you will need to put your code in that page,
and since you cannot use .htaccess and the page is SHTML, you cannot put php in that, you a Javascript Redirection from 404.shtml to your 404.php might do the trick,
Hope that helps.
It looks like that you do not want to redirect but to include the page in question. The redirect is not necessary. Infact it will undermine to use the apache 404 error handler for nice urls.
The following example script does first resolve the request to a (relative) filename, the module. In your code that would be new-url/based-on/some-logic.php
or similar.
Next to that, if a module is not found, an error page template will be used instead. I have named it error404.php
so you would additionally need to create that file.
As a last resort, if even no error template is found, a standard 404 error message will be returned.
<?php
// === resolver ===
// put your logic in here to resolve the PHP file,
// return false if there is no module for the request (404).
function resolveModule() {
// return false;
return 'new-url/based-on/some-logic.php';
}
// returns the filename of a module
// or false if things failed.
function resolveFile($module) {
$status = 200;
if (false === $module) {
$status = 404;
$module = 'error404.php';
}
// modules are files relative to current directory
$path = realpath($module);
if (!file_exists($path)) {
// hard 404 error.
$status = 404;
$path = false;
}
return array($path, $status);
}
// process the input request and resolve it
// to a file to load.
$module = resolveModule();
list($path, $status) = resolveFile($module);
// === loader ===
// send status message
header($_SERVER['SERVER_PROTOCOL'] . ' ' . $status, true, $status);
if (false !== $path) {
include($path); // include module file (instead of redirect)
} else {
// hard 404 error, e.g. the error page is not even found (misconfiguration)
?>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404:</h1>
<h3>Sorry, the page you have requested has not been found.</h3>
<p>Additionally the good looking error page is not available, so it looks really sad.</p>
</body>
</html>
<?
}
精彩评论