18910140161

PHP-如何使用.htaccess文件制作自定义错误页面-堆栈溢出

顺晟科技

2022-10-18 14:03:26

190

我正在尝试为每个错误引发一个自定义错误

页面。

这是我的目录。

.htaccess
error_pages:
           error_400.html
           error_401.html
           error_404.html

顺晟科技:

你很接近了。首先,如果你还没有这样做,你需要初始化你的.htaccess文档。

RewriteEngine On

接下来,您需要定义错误并告诉.htaccess使用哪个文件来处理该错误:

ErrorDocument 400 /error_pages/error_400.html
ErrorDocument 401 /error_pages/error_401.html
ErrorDocument 404 /error_pages/error_404.html

希望这能有所帮助.

您将问题标记为php,但在示例中使用了.html?如果您使用的是PHP,那么您不需要为每个HTTP状态创建单独的错误文档。您可以使用相同的PHP错误文档并检查REDIRECT_STATUS(即,$_SERVER['REDIRECT_STATUS']SUPERGLOBAL)以确定触发错误文档的错误的状态。

因此,您的.htaccess文件将包含:

ErrorDocument 400 /error_pages/error_handler.php
ErrorDocument 401 /error_pages/error_handler.php
ErrorDocument 404 /error_pages/error_handler.php

参考资料:

    https://httpd.apache.org/docs/2.4/custom-error.html.
  • https://httpd.apache.org/docs/2.4/mod/core.html#errordocument.
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航