用于捕获所有错误的单个 ErrorDocument 指令 (.htaccess)

发布于 2024-08-28 07:41:51 字数 257 浏览 5 评论 0 原文

是否有类似通配符指令的东西可以捕获所有可能的错误并在单个自定义错误页面中处理它们?

ErrorDocument 404 /error.php?code=404
ErrorDocument 403 /error.php?code=403
...
ErrorDocument NNN /error.php?code=NNN #possible use of RegExp?

我知道我可能不会在这里处理很多自定义错误页面,但我对此很好奇。

Is there something like a wildcard directive to catch all possible errors and deal with them in a single custom error page?

ErrorDocument 404 /error.php?code=404
ErrorDocument 403 /error.php?code=403
...
ErrorDocument NNN /error.php?code=NNN #possible use of RegExp?

I know I probably won't be dealing with a lot of custom error pages here, but I'm curious about this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼泪也成诗 2024-09-04 07:41:51

那是不可能的。对于要以不同于默认错误处理程序的方式处理的每个状态代码,您需要有一个 ErrorDocument 指令。

That is not possible. You need to have a ErrorDocument directive for each status code you want to handle differently than with the default error handler.

爱的十字路口 2024-09-04 07:41:51

完整参考...据我所知...

首先 - .htaccess 格式

第二 - 说明。

所有这些我都在这里找到 -> httpstatuses.com

这些都是代码 - 不仅仅是错误
.. 有些会破坏您的网站。 。

使用方式如下:

ErrorDocument 100 https://EXAMPLE.com

这是精确的副本粘贴..它不是我的,但我重新格式化了列表。


(将鼠标悬停查看说明,单击查看详细信息。)

1××:信息

100:继续
101:切换协议
102:处理


2××:成功

200:确定
201:已创建
202:接受
203:非权威信息
204:好的(无内容)
205:重置内容
206:部分内容
207 :多状态
< strong>208:已举报
226:使用 IM


3××:重定向

300:多种选择
301:永久移动
302:找到
303:参见其他
304:未修改
305:使用代理
307:临时重定向
308:永久重定向


4××:客户端错误

400:错误请求
401:未经授权
402:需要付款
403:禁止
< strong>404:未找到
405:不允许使用方法
406:不可接受
407:需要代理身份验证
408< /strong> : 请求超时
409:冲突
410:走了
411 :需要长度
412< /strong> : 前提条件失败
413:请求实体太大
< strong>414
:请求 URI 太长
415:不支持的媒体类型
416:请求的范围无法满足
417:期望失败
418:我是一个茶壶
422:无法处理的实体
423 :已锁定
424:方法失败
426:需要升级
428:先决条件必填

431:请求标头字段太大
444:无响应
451 :由于法律原因无法使用
499:客户端关闭请求


5××:服务器错误

500:内部服务器错误
501:未实施
502:错误网关
503:服务不可用
504:网关超时
505:不支持 HTTP 版本
506:变体也进行谈判
507:存储空间不足
508:检测到循环
510:未延期
511:网络需要身份验证
599:网络连接超时错误

(来源)

For full reference... As far as i could get...

First - .htaccess formatted

Second - Descriptions.

All these i found here -> httpstatuses.com

These are All codes - Not just Errors
.. Some will break your site. .

Use like:

ErrorDocument 100 https://EXAMPLE.com

This is the exact copy Paste.. It is not mine, however i reformatted the list.


( Hover for description, Click for detail. )

1×× : Informational

100 : Continue
101 : Switching Protocols
102 : Processing


2×× : Success

200 : OK
201 : Created
202 : Accepted
203 : Non-Authoritative Information
204 : OK (No Content)
205 : Reset Content
206 : Partial Content
207 : Multi-Status
208 : Already Reported
226 : IM Used


3×× : Redirection

300 : Multiple Choices
301 : Moved Permanently
302 : Found
303 : See Other
304 : Not Modified
305 : Use Proxy
307 : Temporary Redirect
308 : Permanent Redirect


4×× : Client Error

400 : Bad Request
401 : Unauthorized
402 : Payment Required
403 : Forbidden
404 : Not Found
405 : Method Not Allowed
406 : Not Acceptable
407 : Proxy Authentication Required
408 : Request Timeout
409 : Conflict
410 : Gone
411 : Length Required
412 : Precondition Failed
413 : Request Entity Too Large
414 : Request-URI Too Long
415 : Unsupported Media Type
416 : Requested Range Not Satisfiable
417 : Expectation Failed
418 : I'm a teapot
422 : Unprocessable Entity
423 : Locked
424 : Method Failure
426 : Upgrade Required
428 : Precondition Required
429.") : Too Many Requests
431 : Request Header Fields Too Large
444 : No Response
451 : Unavailable For Legal Reasons
499 : Client Closed Request


5×× : Server Error

500 : Internal Server Error
501 : Not Implemented
502 : Bad Gateway
503 : Service Unavailable
504 : Gateway Timeout
505 : HTTP Version Not Supported
506 : Variant Also Negotiates
507 : Insufficient Storage
508 : Loop Detected
510 : Not Extended
511 : Network Authentication Required
599 : Network connect timeout error

(Source)

倾城月光淡如水﹏ 2024-09-04 07:41:51

感谢@gadget-guru,你启发我构建了一个脚本来测试所有错误,并构建了这个 .htaccess 文件来一次捕获所有错误。

ErrorDocument 400 /
ErrorDocument 401 /
ErrorDocument 402 /
ErrorDocument 403 /
ErrorDocument 404 /
ErrorDocument 405 /
ErrorDocument 406 /
ErrorDocument 407 /
ErrorDocument 408 /
ErrorDocument 409 /
ErrorDocument 410 /
ErrorDocument 411 /
ErrorDocument 412 /
ErrorDocument 413 /
ErrorDocument 414 /
ErrorDocument 415 /
ErrorDocument 416 /
ErrorDocument 417 /
ErrorDocument 422 /
ErrorDocument 423 /
ErrorDocument 424 /
ErrorDocument 426 /
ErrorDocument 428 /
ErrorDocument 429 /
ErrorDocument 431 /
ErrorDocument 451 /
ErrorDocument 500 /
ErrorDocument 501 /
ErrorDocument 502 /
ErrorDocument 503 /
ErrorDocument 504 /
ErrorDocument 505 /
ErrorDocument 506 /
ErrorDocument 507 /
ErrorDocument 508 /
ErrorDocument 510 /
ErrorDocument 511 /

这会将用户重定向到主页,但您当然可以选择任何页面。示例:

ErrorDocument 511 /error.html

注意任何其他错误代码都会破坏服务器,导致 HTTP 500

Thanks @gadget-guru you inspired me to build a script to test all errors and built this .htaccess file to catch all errors at once.

ErrorDocument 400 /
ErrorDocument 401 /
ErrorDocument 402 /
ErrorDocument 403 /
ErrorDocument 404 /
ErrorDocument 405 /
ErrorDocument 406 /
ErrorDocument 407 /
ErrorDocument 408 /
ErrorDocument 409 /
ErrorDocument 410 /
ErrorDocument 411 /
ErrorDocument 412 /
ErrorDocument 413 /
ErrorDocument 414 /
ErrorDocument 415 /
ErrorDocument 416 /
ErrorDocument 417 /
ErrorDocument 422 /
ErrorDocument 423 /
ErrorDocument 424 /
ErrorDocument 426 /
ErrorDocument 428 /
ErrorDocument 429 /
ErrorDocument 431 /
ErrorDocument 451 /
ErrorDocument 500 /
ErrorDocument 501 /
ErrorDocument 502 /
ErrorDocument 503 /
ErrorDocument 504 /
ErrorDocument 505 /
ErrorDocument 506 /
ErrorDocument 507 /
ErrorDocument 508 /
ErrorDocument 510 /
ErrorDocument 511 /

this will redirect the user to the main page, but you can choose any page of course. Example:

ErrorDocument 511 /error.html

note that any other error code will break the server causing HTTP 500

白况 2024-09-04 07:41:51

您可以通过 REDIRECT_STATUS 环境变量(由 Apache 设置)在脚本中获取它,而不是传递错误状态代码。请参阅:http://httpd.apache.org/docs/2.0/custom-error .html

Rather than passing in the error status code, you can pick it up in your script via the REDIRECT_STATUS environment variable (set by Apache). See: http://httpd.apache.org/docs/2.0/custom-error.html

做个ˇ局外人 2024-09-04 07:41:51

我没有找到一种方法来用一行处理所有 ErrorDocument。但您不必写出所有重定向。您可以通过以下方式在 PHP 中传递错误代码:

$response_code = http_response_code()

我的 .htaccess 看起来像

ErrorDocument 401 /error.php
ErrorDocument 402 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

I don't found a way to handle all ErrorDocument's with one line. But you dont have to write out all the redirects. You can pass the ErrorCode in PHP with:

$response_code = http_response_code()

My .htaccess looks like

ErrorDocument 401 /error.php
ErrorDocument 402 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文