我必须使用自定义404页面进行url重定向(.htaccess不是一个选项)。我正在考虑使用下面的代码。我试图通过在URL中添加一个标志来解释有效的重定向和未找到的页面的真实情况。你认为如何?
<?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>编辑-- .htaccess之所以不是一个选项,是因为我在没有.htaccess的情况下在IIS6上运行。
发布于 2011-06-15 09:58:56
添加404个标题生成:
...
header("HTTP/1.0 404 Not Found");
// for FastCGI: header("Status: 404 Not Found");
// no redirection - display the 'not found' page...
?>
...并删除200个代码:
// 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.发布于 2011-06-15 10:05:18
如果我没有搞错你,你希望404页显示一个页面,也就是说,不再存在的页面。
据我所知(可能不是太多),您不能仅仅将php页面设置为404处理程序,而不需要使用.htaccess或apache文件。
我记得404.shtml是普通apache设置中404处理程序的默认文件,所以您需要将代码放在该页面中,
由于您不能使用.htaccess,并且页面是SHTML,所以不能将php放在其中,所以Javascript重定向从404.shtml到您的404.php可能会奏效,
希望这能有所帮助。
发布于 2011-06-15 10:15:11
看起来,您不想重定向,而是要包含所讨论的页面。重定向是不必要的。实际上,将Apache404错误处理程序用于漂亮的urls会破坏它。
下面的示例脚本首先将请求解析为(相对)文件名,即模块。在您的代码中,这将是new-url/based-on/some-logic.php或类似的。
接下来,如果找不到模块,则将使用错误页面模板。我已经将它命名为error404.php,因此您还需要创建该文件。
作为最后手段,即使没有找到错误模板,也会返回一个标准404错误消息。
<?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>
<?
}https://stackoverflow.com/questions/6355925
复制相似问题