首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于404重定向,正确的PHP?

对于404重定向,正确的PHP?
EN

Stack Overflow用户
提问于 2011-06-15 09:54:06
回答 3查看 4.4K关注 0票数 2

我必须使用自定义404页面进行url重定向(.htaccess不是一个选项)。我正在考虑使用下面的代码。我试图通过在URL中添加一个标志来解释有效的重定向和未找到的页面的真实情况。你认为如何?

代码语言:javascript
复制
<?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上运行。

EN

回答 3

Stack Overflow用户

发布于 2011-06-15 09:58:56

添加404个标题生成:

代码语言:javascript
复制
...
header("HTTP/1.0 404 Not Found");
// for FastCGI: header("Status: 404 Not Found");
// no redirection - display the 'not found' page...
?>
...

并删除200个代码:

代码语言:javascript
复制
// 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.
票数 4
EN

Stack Overflow用户

发布于 2011-06-15 10:05:18

如果我没有搞错你,你希望404页显示一个页面,也就是说,不再存在的页面。

据我所知(可能不是太多),您不能仅仅将php页面设置为404处理程序,而不需要使用.htaccess或apache文件。

我记得404.shtml是普通apache设置中404处理程序的默认文件,所以您需要将代码放在该页面中,

由于您不能使用.htaccess,并且页面是SHTML,所以不能将php放在其中,所以Javascript重定向从404.shtml到您的404.php可能会奏效,

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2011-06-15 10:15:11

看起来,您不想重定向,而是要包含所讨论的页面。重定向是不必要的。实际上,将Apache404错误处理程序用于漂亮的urls会破坏它。

下面的示例脚本首先将请求解析为(相对)文件名,即模块。在您的代码中,这将是new-url/based-on/some-logic.php或类似的。

接下来,如果找不到模块,则将使用错误页面模板。我已经将它命名为error404.php,因此您还需要创建该文件。

作为最后手段,即使没有找到错误模板,也会返回一个标准404错误消息。

代码语言:javascript
复制
<?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>
<?
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6355925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档