How to Enable PHP Error Logging via htaccess
In this brief tutorial, I will show Apache users how to suppress PHP errors from visitors and enable PHP error logging via htaccess.
Tracking your site’s PHP errors is an excellent way to manage and troubleshoot unexpected issues related to plugins and themes. Even better, monitoring PHP errors behind the scenes via private log is far better than trying to catch them as they appear at random visits. Thanks to the magical powers of htaccess, there is an easy way to implement this effective strategy.
Hide PHP errors from visitors
In our article, , we discuss a technique whereby PHP errors are suppressed via htaccess. This is done by including the following htaccess directives to your domain’s httpd.conf or to your site’s root (or other target directory) htaccess file:
# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
With that in place, PHP errors will no longer be displayed publicly on your site. This eliminates a potential security risk, and keeps those ugly, unintelligible PHP errors from breaking your site layout and disorienting your visitors. No editing required for this code.
Enable private PHP error logging
Now that we have hidden PHP errors from public view, let’s enable the logging of PHP errors so that we can privately keep track of them. This is done by including the following htaccess directives to your domain’s httpd.conf or to your site’s root (or other target directory) htaccess file:
# enable PHP error logging
php_flag log_errors on
php_value error_log /home/path/public_html/domain/PHP_errors.log
For this to work, you will need to edit the path in the last line to reflect the actual location of your PHP_errors.log file. Of course, you will need to create this file and subsequently set the file permissions to 755 or, if necessary, 777. Finally, you need to secure the log file itself by adding this final line of code to your htaccess file:
# prevent access to PHP error log
<Files PHP_errors.log>
Order allow,deny
Deny from all
Satisfy All
</Files>
Then, after everything is in place, check that everything is working by triggering a few PHP errors. You may also want to verify protection of your error log by trying to access it via a browser.
Related articles
- Eliminate 404 Errors for PHP Functions
- Roll your own Apache Rewrite Log
- Permanently Redirect a Specific IP Request for a Single Page via htaccess
- Temporary Site Redirect for Visitors during Site Updates
- htaccess Combo Pack: WordPress Permalinks and non-www Redirect
- Stupid htaccess Trick: Enable File or Directory Access to Your Password-Protected Site
- Blacklist Candidate Number 2008-01-02
About this article
This is article #462, posted by Perishable on Monday, December 17, 2007 @ 08:45am. Categorized as Function, and tagged with apache, error, htaccess, log, php, tips, tricks. Updated on December 18, 2007. Visited 19831 times. 11 Responses »
Bookmark • Trackback • Comment • Subscribe • Explore
« Yahoo! Slurp in My Blackhole (Yet Again) • Up • Optimize WordPress: Pure Code Alternatives for 7 Unnecessary Plugins »
1 • January 24, 2008 at 7:19 pm — jonathan says:
Yr codes let my .htaccess spoilt…
[ Editor’s note: comment edited to remove the redundant list of PHP errors.. ]