【问题标题】:redirect to my custom404 error page php重定向到我的 custom404 错误页面 php
【发布时间】:2013-04-18 06:21:03
【问题描述】:

我的根目录中有一个自定义的 404 错误页面。

我也编辑了我的 .httaccess 文件。

现在我想重定向到我的自定义 404 页面,如果我的页面中某些条件失败。

例如 $_SESSION[log]==1 失败。 所以我的问题是

1.我应该在php中使用什么代码来做到这一点?

2.我如何犯其他类型的错误,如 401 和许多其他类型的错误?

在我的 .httaccess 页面中,我有这些行

ErrorDocument 404 http://mydomain.com/404custom.php

我有一个 404custom.php 根据我的需要设计..简单的 HTML 页面...

【问题讨论】:

  • 请显示您目前拥有的代码

标签: php redirect error-handling http-status-code-404


【解决方案1】:

在错误情况下不要发送 Location 标头。而是发送正确的状态代码。您必须根据 http 规范发送正确的状态码。

header("HTTP/1.0 404 Not Found");

为了显示正确的自定义站点,您已经在 .htaccess 文件中包含了 ErrorDocument 语句。

尤其对于 403 未授权错误,发送正确的状态码很重要,这样浏览器才能做出准确的反应。

【讨论】:

    【解决方案2】:

    1- 要重定向您的用户,您可以使用:

    <?php
       header('Location: http://www.example.com/404.html');
    ?>
    

    但是在使用header()之前不能发送任何数据,不能使用echo,甚至不能使用空格。

    2- 对于其他错误:

    ErrorDocument 400 /var/www/errors/index.html
    ErrorDocument 401 /var/www/errors/index.html
    ErrorDocument 403 /var/www/errors/index.html
    ErrorDocument 404 /var/www/errors/index.html
    ErrorDocument 500 /var/www/errors/index.html
    

    【讨论】:

    • 可能还值得发送一个 HTTP 标头代码让浏览器知道发生了什么。 en.wikipedia.org/wiki/List_of_HTTP_status_codes
    • @Chewie 感谢您的回复.. 404.html?我的意思是我必须使用我的页面(404custom.php)。或者 404.html 是标准页面..
    • 404custom.php 也可以。只需提供您要将用户重定向到的网址。 @JamesElliott 我完全同意,您可以参考 header() 手册来发送 HTTP 响应代码。 php.net/manual/fr/function.header.php
    猜你喜欢
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 2011-11-21
    • 2013-06-09
    • 1970-01-01
    • 2016-07-22
    相关资源
    最近更新 更多