如何在 PHP / Kohana 中捕获 Apache 的 403 错误?
我的网站在 Kohana v3 上运行,我在 bootstrap.php 中设置了自定义错误页面,如下所示,
try
{
// Attempt to execute the response
$request->execute();
}
catch (Exception $e)
{
.... error page re-direct.
}
它可以正常处理 404 错误。
我还有一些无法直接访问的文件夹,但它们必须位于 www 根目录下。我放置了 .htaccess 来保护这些文件夹
# nobody can access this folder from Browser
Deny from all
当用户在浏览器中输入 URL 来访问此文件夹下的文件时,将以纯文本形式显示 Apache 403 错误,如下所示:
Forbidden
You don't 无权访问 /private_folder on这个服务器。
我想自定义这个错误并在 Kohana 中处理它。但是,我发现Apache完全控制了403错误,在这种情况下甚至不调用index.php。
有什么想法可以解决吗?
My website is running on Kohana v3, and I have set up customized error page like below in bootstrap.php
try
{
// Attempt to execute the response
$request->execute();
}
catch (Exception $e)
{
.... error page re-direct.
}
It works fine with 404 error.
I also have some folders that cannot be directly accessed but they have to be located under the www root. I put the .htaccess to protect these folders
# nobody can access this folder from Browser
Deny from all
When user type in the URL in the browser to access files under this folder, an Apache 403 error will be shown in plain text like this:
Forbidden
You don't have permission to access /private_folder on this server.
I want to customize this error and handle it in Kohana. However, I find that Apache fully controls the 403 error, and does not even call the index.php under this situation.
Any idea to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用代码来完成此操作,而不是使用 .htaccess 来锁定该区域。 PHP 的标头函数可用于发送 403 状态。
另一种方法是为 Apache 中的 403 错误设置自定义错误文档,并将其指向处理错误的脚本。有关如何执行此操作的信息,请参阅 ErrorDocument 文档,但它将会是这样的:
Instead of using .htaccess to lock that area down, you could do it in code. PHP's header function could be used to send the 403 status.
Another approach would be to set up a custom error document for 403 errors in Apache, and point this at a script to handle the error. Refer to the ErrorDocument documentation on how to do this, but it would be something along these lines: