Enabling and disabling PHP error logging using an .htaccess file
The log_errors directive in PHP controls whether error messages are logged to a file. The error_log directive specifies the location of the log file. On ruachost.com, you can configure these settings in a custom .htaccess file when you don’t have direct access to php.ini.
Why Manage Error Logging?
Section titled “Why Manage Error Logging?”- Enable logging: Capture errors for debugging and troubleshooting.
- Disable logging: Reduce disk usage or prevent unnecessary logs in production.
- Security: Keep error details out of public view while still recording them privately.
Steps to Configure Error Logging in .htaccess
Section titled “Steps to Configure Error Logging in .htaccess”Step 1: Access Your Hosting Account
Section titled “Step 1: Access Your Hosting Account”- Log in to cPanel or use FTP/File Manager.
- Navigate to your site’s root directory (e.g.,
/public_html).
Step 2: Create or Edit .htaccess
Section titled “Step 2: Create or Edit .htaccess”- If
.htaccessalready exists, open it for editing. - If not, create a new file named
.htaccess.
Step 3: Add Directives
Section titled “Step 3: Add Directives”✅ Enable Error Logging
Section titled “✅ Enable Error Logging”php_flag log_errors Onphp_value error_log /home/USERNAME/logs/php_errors.log- Replace
/home/USERNAME/logs/php_errors.logwith the full path to your desired log file. - Ensure the directory exists and is writable.
❌ Disable Error Logging
Section titled “❌ Disable Error Logging”php_flag log_errors OffStep 4: Save and Verify
Section titled “Step 4: Save and Verify”-
Save the
.htaccessfile. -
Create a test file with an intentional error:
<?php echo $undefined_variable; ?> -
Load it in your browser.
- If logging is enabled, check the log file for entries.
- If disabled, no new entries should appear.
Best Practices
Section titled “Best Practices”- Keep logs outside the public web root for security.
- Rotate or clear logs regularly to prevent large files.
- Use
display_errors = Offin production to avoid exposing sensitive details.