Disable PHP Execution & Directory Browsing in WordPress

Disable PHP Execution & Directory Browsing in WordPress

With so many threats out there, WordPress site security should be a top priority for site owners. Hackers often exploit vulnerabilities through PHP execution or by gaining access to a site’s directory structure to uncover weak points. One effective way to protect your site is by restricting PHP from running in vulnerable folders and disabling directory browsing. This guide will walk you through these steps to help secure your WordPress site, so you can feel confident that it’s well-protected.


PHP is the backbone of many dynamic features in content management systems like WordPress. However, leaving PHP execution enabled in folders like /wp-content/uploads/ can open the door for hackers to upload and execute harmful scripts. Disabling PHP execution in non-essential folders reduces this risk significantly.

Directory browsing exposes your site’s folder structure and contents, making it easier for malicious actors to locate sensitive files, such as backups or configuration files. By disabling directory browsing, you keep your site’s structure private and better protected.


Before making any changes to your site files, create a backup to protect your data if you need to restore it later.

1
Log into your hosting provider’s cPanel.
2
Under the Files section, select Backup or Backup Wizard.
3
Follow the prompts to create a full backup of your website, including databases and files.
1
Install a backup plugin like UpdraftPlus or BackWPup.
2
Follow the plugin instructions to generate a backup and store it safely.

To disable PHP execution in certain directories, we will modify the .htaccess file, commonly used on Apache servers.

1

Access Your Site Files:
Use an FTP client (e.g., FileZilla) or your hosting provider’s file manager to access the root directory of your WordPress site.

2

Navigate to Vulnerable Directories:
Target directories where PHP execution isn’t necessary, such as /wp-content/uploads/ and /wp-includes/.

3

Create or Edit the .htaccess File:
Inside each target directory, create a new file named .htaccess if it doesn’t exist. Open this file in a text editor and add the following code:

2.1 How to Disable PHP Execution Using the .htaccess File
4

Save and Upload:
Save the file and upload it to the directory. Repeat for each directory where PHP execution should be blocked.

5

Confirm Changes:
To verify, try accessing a PHP file in the protected directory (e.g., yoursite.com/wp-content/uploads/test.php). You should see a 403 Forbidden error.

Tip: If you see an error or your site malfunctions, revert to your backup, check the .htaccess syntax, or consult your hosting provider.


Directory browsing allows users to see the contents of a directory, which is often risky. Here’s how to disable it.

1
Open the Root Directory:
Use FTP or your hosting’s file manager to access the root directory of your WordPress site.
2
Open the .htaccess File:
If a .htaccess file exists, open it for editing. Otherwise, create a new .htaccess file.
3
Add the Following Code:
Paste this line at the end of the file:
3.1 Disabling Directory Browsing with .htaccess
4
Save and Upload:
Save and upload the updated .htaccess file to your root directory.
5
Verify Changes:
Test by visiting a directory on your site (e.g., yoursite.com/wp-content/). A 403 Forbidden error confirms directory browsing is disabled.

Tip: If you see an error or your site malfunctions, revert to your backup, check the .htaccess syntax, or consult your hosting provider.


For those preferring a no-code approach, plugins offer a straightforward solution.

1
Choose a Security Plugin:
Plugins like Sucuri Security, iThemes Security, or All In One WP Security provide the necessary settings.
2
Installation and Setup:
– Install your preferred plugin via Plugins > Add New in the WordPress dashboard.
– Activate the plugin, and access its settings.
3
Enable PHP and Directory Browsing Restrictions:
– In Sucuri Security:
Go to Settings > Hardening and select Block PHP Files in Uploads Directory and Disable Directory Browsing.
– In iThemes Security:
Go to Security Settings and find System Tweaks. Check options like PHP in Uploads and Directory Browsing to disable them.

Note: Each plugin’s settings may differ slightly. Always refer to the plugin’s documentation for specifics on enabling these features.


Errors can sometimes occur when editing .htaccess. Here are some common issues and solutions:

  • Syntax Errors: Double-check the .htaccess syntax. A misplaced character could prevent the file from working.
  • File Permission Issues: Ensure the .htaccess file has the correct permissions (usually 644 or 755).
  • Conflicting Plugins: Some plugins might conflict with .htaccess settings. If you encounter issues, try disabling plugins one by one.
  • Reverting Changes: If all else fails, restore from your backup and try again.

Tip: Reach out to your hosting provider if errors persist. They can offer insights or resolve issues directly.


To further secure your WordPress site, consider these best practices:

Use Strong Passwords and Two-Factor Authentication (2FA):

Use Strong Passwords and Two-Factor Authentication (2FA):

Secure admin logins with strong, unique passwords and enable 2FA for added protection.

Install a Web Application Firewall (WAF):

Install a Web Application Firewall (WAF):

A firewall like Sucuri’s WAF filters malicious traffic, blocks threats, and shields your site from vulnerabilities.

Keep WordPress and Plugins Updated:

Keep WordPress and Plugins Updated:

Outdated themes or plugins can introduce security flaws, so regularly update your site components.

Regular Backups:

Regular Backups:

Backup your site at regular intervals to ensure data can be restored if an incident occurs.

Taking steps like disabling directory browsing and blocking PHP execution in non-essential directories can greatly reduce security risks and prevent unauthorized access to your WordPress site. These straightforward safety practices provide a strong foundation for a more secure site and can help limit unwanted attempts to breach it.

For even more comprehensive protection, consider using Sucuri’s solutions, including malware scanning and firewalls. Visit Sucuri.net to explore additional ways to safeguard your website. We’re here to help you protect your site so you can focus on what matters most.


Ready to Join?

We’re dedicated to our partner’s success. Allow us to help your firm with all your customer hosting needs. Peace of mind starts here.