DrivenHost

Blog / security monday

How to block PHP in your WordPress uploads folder

Most WordPress backdoors end up in wp-content/uploads. Here is the one .htaccess rule that stops them running, and how to check that it worked.

This week's security job takes about five minutes and needs one small file. Your uploads folder holds images, PDFs and the odd spreadsheet. None of that is code, so nothing in there should ever run as code. On a default WordPress install, it can.

Why uploads is the favourite hiding place

WordPress needs wp-content/uploads to be writable, because that is where the media library puts everything. Attackers know this, and they know something else about it: a WordPress core update replaces wp-admin and wp-includes, but it never touches uploads. A file dropped in there survives every update you run afterwards.

So when a vulnerable plugin lets someone write a file to the account, uploads is usually where it lands. The file is often small, maybe a few hundred bytes, with a name like a thumbnail or a cache file. On its own it does nothing. The attacker then requests it in a browser, PHP runs it, and they have a shell on your site. That is the pattern behind most of the reinfections we see, where a site gets cleaned up and is back to sending spam a week later.

The fix is not clever. Tell the web server to refuse any request for a PHP file inside that folder, and the file becomes a dead weight sitting on disk.

The rule that closes it

Create a file called .htaccess inside wp-content/uploads with this in it:

<FilesMatch "\.(?i:php|phar|phtml|php[0-9]|phps)$">
    Require all denied
</FilesMatch>

In cPanel, open File Manager, go to Settings in the top right and tick "Show hidden files" so you can see .htaccess files at all. Navigate to public_html/wp-content/uploads, use New File, name it .htaccess, then edit it and paste the block in. If a .htaccess file is already there, add the block at the end rather than replacing what is in it.

Two things worth knowing. Apache allows FilesMatch inside .htaccess files, which is what makes this work without root access on a shared account. And the rule applies to every folder below it, so your 2024/09 and 2026/11 subfolders are covered by the one file. WordPress does not manage that file either, so your own updates will not overwrite it.

The deny pattern itself is the same one WordPress uses in its own hardening guide to protect wp-config.php. Nothing exotic, just pointed at a different target.

Skip the php_flag trick

Older guides tell you to put php_flag engine off in that folder instead. Do not bother. The PHP manual documents php_value and php_flag under running PHP as an Apache module, and most cPanel accounts now run PHP through FPM or LiteSpeed's handler rather than as a module. Where that directive is not available, you get one of two outcomes: it is quietly ignored and you walk away thinking the folder is protected, or Apache rejects the unknown directive and your whole site returns a 500 error.

Denying by filename avoids the question. The request is refused before anything decides which PHP interpreter to hand it to.

Check that it actually worked

Assuming a rule works is how people end up with a false sense of a locked door, so test it.

Make a plain text file on your computer, put the words "deny test" in it, save it as denytest.php, and upload it to wp-content/uploads with File Manager. Then visit yoursite.ca/wp-content/uploads/denytest.php in a browser. You want a 403 Forbidden page. If you see the words "deny test" instead, the rule is not applying and the folder is still open. Delete the test file either way once you are done.

While you are in there, see what is already sitting in the folder. In File Manager, open uploads and use the search box with .php in it. On a healthy site you should find nothing at all. If something turns up, resist the urge to delete it and call the job finished, because the file is a symptom. Our walkthrough of what a hacked site looks like from the server side covers what else to check before you trust the site again.

What this does not fix

This closes a route, not the hole that let the file in. Whatever wrote that PHP file into your uploads folder is still there until you update or remove it, and loose permissions make the problem worse, which we went through in what file permissions WordPress should use. Treat the uploads rule as one layer among several.

One rare exception: a handful of plugins do generate PHP inside uploads for caching or exports. It is uncommon, but if a feature stops working right after you add the rule, check your error log for a 403 on a path under uploads before you assume the two are unrelated.

If you are on our shared hosting and would rather not edit .htaccess yourself, or you searched your uploads folder and found a PHP file you cannot account for, open a ticket with our support team and we will take a look at the account with you.

Sources

security mondaywordpresshtaccess

← All posts

No sales team. No scripts.

Tell us what you're running.
We'll tell you what it needs.

Migrations are free, quotes are written, and the engineer who replies is on the team that will run your server.