Block PHP execution in a directory using .htaccess

Search another help topic

You can stop browsers from running PHP files in a specific folder by adding a short rule to that folder’s .htaccess file. This is a common way to protect upload folders from harmful scripts.

An .htaccess file is a small configuration file used on Apache servers. Rules in it apply to the folder it sits in and the folders below it.

Prerequisites

  • You’ll need a Ecenica hosting account that supports .htaccess.
  • You’ll need access to your files by File Manager or FTP.
  • You’ll need the path of the folder you want to protect, for example /wp-content/uploads.

Steps

1. Open the folder you want to protect

  1. Open File Manager or connect by FTP.
  2. Go to the folder where PHP files should never run directly, for example public_html/wp-content/uploads.
  3. Check if an .htaccess file already exists in that folder.
  4. If hidden files are off, turn on Show Hidden Files. See How-to use File Manager on cPanel for where to find this setting.

2. Create or edit the .htaccess file

  1. If no .htaccess file exists, create a new file named .htaccess in that folder.
  2. If one exists, download a copy as a backup.
  3. Open the .htaccess file for editing. In File Manager, select the file and click Edit.

3. Add the block rule

  1. Paste the following code into the file.
  2. Save the file.
# Block PHP/PHP-like files from being accessed in this directory
<FilesMatch "\.(?i:php[0-9]*|phtml|phar|phps)$">
    Require all denied
</FilesMatch>

This rule matches .php, .php5, .php8, .phtml, .phar, and .phps files. The (?i:...) part makes the match case-insensitive, so .PHP is also blocked. Visitors who request a matching file get a 403 Forbidden error.

4. Test the rule

  1. Upload a small test file named test.php to the protected folder, if one isn’t there already.
  2. Visit it in your browser at `https://example.com/uploads/test.php`.
  3. Confirm you see 403 Forbidden.
  4. Delete the test file.
  5. Load a normal page and image from your site to confirm nothing else is blocked.

What this rule does and does not do

This rule blocks direct web access to PHP files in that folder. It does not delete the files.

PHP files in the protected folder can still be included by code running elsewhere on your site. If you need to stop all use of a file, delete it.

Place the .htaccess file only in folders that should never serve PHP directly, such as uploads, cache, or images. Do not place it in your site root or in /wp-admin, or your site may stop working.

Troubleshooting / FAQs

Will this break WordPress?
No, if placed in wp-content/uploads. Do not place it in the site root.

I see a 500 error after saving. What should I check?
Check for typos in <FilesMatch> and </FilesMatch>. Restore your backup copy, then paste the code again.

My whole site shows 403. How do I fix it?
You likely added the file to the wrong folder. Delete or move that .htaccess file, then clear your browser cache and test again.

Does this protect subfolders too?
Yes. Rules apply to the folder it’s in and to all subfolders below it.

Should I still delete unknown PHP files in uploads?
Yes. Blocking access helps, but you should still delete files you did not upload. If you need help, contact Ecenica support.

Related articles

Need help with this?

Open a ticket and link to this guide so our team can see what you have tried.

View support tickets

Power your business with Ecenica Hosting

Built for WordPress and serious websites. Fast, secure and supported by real people in the UK.

Ecenica hosting services represented by a connected red route across London