【问题标题】:Catching errors using .htaccess [closed]使用 .htaccess 捕获错误 [关闭]
【发布时间】:2012-02-21 03:33:23
【问题描述】:

我正在尝试编写一个 .htaccess 文件来定向并发送带有错误代码的索引。例如 index.php?error404 或 index.php?error501。

我可以使用下面的代码在根目录中引导错误。

ErrorDocument 404 index.php?error=404
ErrorDocument 501 index.php?error=501

例如http://www.domain.com/file_does_not_exist.php 并且这有效。 结果http://www.domain.com/index.php?error=404

问题是从子目录捕获并发送到 index.php 错误。

例如http://www.domain.com/directory_does_not_existhttp://www.domain.com/directory_and/file_does_not_exist.php.

结果是http://www.domain.com/directory_does_not_exist/index.php?error=404,因此 index.php 的 CSS 损坏了。

我尝试了下面的代码但没有成功。我正在尝试通过重写 URL 来重定向回 index.php。

RewriteCond $1 !^err[45][0-9][0-9]$
RewriteRule ^\/.\/\.php$ /index.php?s=$1 [R]

AND

RewriteCond $1 !^err[45][0-9][0-9]$
RewriteRule ^([/]?*)\.php$ /index.php?s=$1 [QSA,L]

任何帮助将不胜感激。

【问题讨论】:

    标签: php regex .htaccess


    【解决方案1】:

    无需使用重写;只需在 ErrorDocument 指令中使用绝对网址(以 / 开头):

    ErrorDocument 404 /index.php?error=404
    ErrorDocument 501 /index.php?error=501
    

    The docs 在示例中显示绝对网址:

    对于本地网络路径(相对于 DocumentRoot),URL 可以以斜杠 (/) 开头,或者是客户端可以解析的完整 URL。或者,可以提供消息以由浏览器显示。例子::

    ErrorDocument 500 http://foo.example.com/cgi-bin/tester
    ErrorDocument 404 /cgi-bin/bad_urls.pl
    ErrorDocument 401 /subscription_info.html
    ErrorDocument 403 "Sorry can't allow you access today"
    

    【讨论】:

      猜你喜欢
      • 2023-01-06
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      相关资源
      最近更新 更多