php - 重定向到我的custom404错误页面php

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

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

我也编辑了.httaccess文件。

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

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

1.我应该在php中使用什么代码呢?

2.如何制作其他类型的错误,例如401和许多其他类型的错误?

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

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

我有一个404custom.php可以满足我的需求。简单的HTML页面...

最佳答案

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

关于php - 重定向到我的custom404错误页面php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16196403/

相关文章:

php - 对数标度 - PHP 排名

php - 来自 mysql 的 Google-charts 日期范围过滤器

python - 如何打印嵌套循环的异常?

python - 词法分析器错误处理 PLY Python

Python 子进程 - 将 stdout/err 重定向到两个地方

com - 对于 “this object is not completely initialized”正确使用HRESULT

php - 拉维尔 : Syntax error or access violation: 1055 Error

javascript - 如何在不刷新页面的情况下使用codeigniter中的websocket更新内容

html - 苹果应用商店角标(Badge)在移动设备上重定向至可疑广告,但在桌面设备上则不然

apache - HAProxy - 将一个域的流量重定向到 https,其他域仅重定向到 http