使用 .htaccess 处理 PHP 错误
我读过几篇有关网站安全的文章,他们建议将此代码添加到您的 .htaccess
文件中,以防止显示 PHP 错误:
# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0
如果我直接 进入我的 .htaccess
文件时,出现 500 内部服务器错误。有什么问题吗?这些都是已弃用的东西吗?
I've been reading a couple of articles on website security and they recommend adding this code to your .htaccess
file to prevent the display of PHP errors:
# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0
If I add this code directly into my .htaccess
file I am given a 500 internal server error. What's wrong with it? Is this all deprecated stuff?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实上,它确实依赖于
PHP SAPI
。这种精确的.htaccess
语法仅适用于mod_php
设置,不适用于CGI
或FastCGI
安装。在后一种情况下,您将使用.user.ini
(对于 PHP 5.3 及以上版本)。
不过,您拥有的大多数选项都可以在运行时配置。在调用脚本顶部使用
ini_set()
:请注意,对于
_startup_errors 那里配置显然已经太晚了。如果
display_errors
已经关闭,那么禁用html_errors
和 docref 也是多余的。It does in fact depend on the
PHP SAPI
. This precise.htaccess
syntax will only work withmod_php
setups, not withCGI
orFastCGI
installations. In the latter case you would use a.user.ini
(for PHP 5.3 onwards) instead.Most of the options you have there can however be configured at runtime. Use
ini_set()
atop the invocation script:Note that for
_startup_errors
it's obviously too late to be configured there. Also it's redundant to disablehtml_errors
and the docref things ifdisplay_errors
is already off.为了安全起见,最好不要显示错误,但请记住确保启用了日志记录,以便您可以看到站点中发生的错误并追踪其来源。
在 .htaccess 中,您可以编写:
另外,由于您使用 .htaccess,您可以拥有自己的自定义错误页面:
For security, it's better not to display the errors but remember to make sure their logging is enabled, so you can see the errors happening in the site and trace their sources.
In .htaccess you can write:
Also since you are using .htaccess, you can have your own custom error pages:
最好的选择是禁用 php.ini 中的错误,因为在某些情况下,如果您通过 .htaccess 执行此操作,则会出现内部服务器错误。 ;)
The best option is to disable the errors from php.ini, because in some cases if you do it vía .htaccess will give an Internal Server Error. ;)