ExpertsGrow logo Get a free quote ↗

Block PHP Execution in WordPress Uploads: 3 Methods

November 18, 2026 · 12 min read
Block PHP Execution in WordPress Uploads: 3 Methods

The uploads folder is where WordPress stores your images, PDFs and other media. It should hold files, not code. To block PHP execution in WordPress uploads, you tell your web server to refuse to run any .php file inside wp-content/uploads/. This guide gives the rule for Apache, Nginx and LiteSpeed, a harmless way to test it, and what can break.

Block php execution in wordpress uploads: Experts Grow guide cover

Quick answer: To block PHP execution in WordPress uploads, add a deny rule for PHP file extensions to the uploads folder. On Apache or LiteSpeed Enterprise use .htaccess. On Nginx add a location block to the server config and reload. Back up first, then test with a harmless PHP file.

Back up before you start. Server rules can take a site offline with one typo. Copy the current .htaccess or Nginx config, and take a full site backup, before changing anything.

Why you should block PHP execution in WordPress uploads

WordPress lets logged-in users and some plugins upload files. Most of the time that is harmless. But if an attacker finds a weak upload form, a vulnerable plugin or a stolen login, a common next step is to place a PHP file in the uploads folder and open its URL. That file is often called a web shell. Once it runs, the attacker can read your files, change content and add spam.

Uploads are also a good place to hide, because the folder is writable by design and fills up with thousands of files that nobody reads.

Stopping PHP from running there does not fix the weak plugin. It does make a common attack much less useful. The file can still be planted, but opening its URL returns an error instead of running the code.

WordPress’s own hardening guide covers file permissions, locking down wp-config.php and disabling the dashboard file editor. It does not include a rule for the uploads folder, so this is a server-level step you add yourself. It also notes that disabling the file editor “will not prevent an attacker from uploading malicious files”, which is exactly why the uploads rule is worth having.

Which way to block PHP execution in WordPress uploads fits your server?

Find out which web server you use before you block PHP execution in WordPress uploads. Your hosting panel, your host’s support page or Tools > Site Health > Info > Server will usually say.

Web serverWhere the rule goesNeeds a restart or reload?
Apache.htaccess in the uploads folder, if overrides are allowedNo
NginxA location block in the server configYes, reload Nginx
LiteSpeed Enterprise.htaccess as on ApacheUsually picks up changes by itself
OpenLiteSpeedA rewrite rule, usually in the virtual hostYes, restart

Many managed WordPress hosts already block PHP execution in WordPress uploads. Ask your host first. If they confirm it, run the test below anyway, because you should trust the result, not the claim.

Method 1: Block PHP execution in WordPress uploads on Apache

Apache reads per-folder settings from .htaccess files. Create a new file named .htaccess inside wp-content/uploads/ using your host’s file manager or SFTP. If one already exists, add to it. Add this:

Test first: We have not run this rule on every Apache setup. Back up your current .htaccess, try it on a staging copy, then request a .php file inside wp-content/uploads and confirm you get 403 or 404 (see the test section below).

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

What it does, line by line:

  • FilesMatch limits the rule to filenames that match a regular expression. The Apache core docs list it as valid in .htaccess.
  • The pattern matches names ending in .php, .php5, .php7, .phtml, .phar or .pht, in any letter case. These are the extensions servers commonly treat as PHP.
  • Require all denied returns a 403 Forbidden error for those files.

Do not paste this into your main .htaccess in the site root. It belongs in the uploads folder so it does not touch themes or plugins.

The AllowOverride catch

This file only works if Apache is allowed to read it. Apache’s default is AllowOverride None, and the docs say that with None, .htaccess files are completely ignored. The server does not even look for them. The Require directive also belongs to the AuthConfig group of overrides.

Most shared hosts allow it. If yours does not, the rule will silently do nothing, which is why the test below matters. Ask your host to allow it, or ask them to add the rule in the server config.

Method 2: Block PHP execution in WordPress uploads on Nginx

Nginx does not read .htaccess at all. You edit the server block for your site, usually in a file under /etc/nginx/sites-available/ or /etc/nginx/conf.d/. If you are on managed hosting, you may need your host to do this.

The basic rule is:

Test first: We could not test this on every server, and the syntax below is checked against the Nginx docs only. Back up your Nginx config, try it on a staging copy, run nginx -t, reload, then request a .php file inside wp-content/uploads and confirm you get 403 or 404.

location ~* ^/wp-content/uploads/.*\.(?:php[0-9]*|phtml|phar|pht)$ {
    deny all;
}

The Nginx access module docs list deny all as valid inside a location. The page does not state the status code. Nginx is generally expected to answer 403 for a denied request, but we did not confirm that on a live server, so check it with your own test.

The ordering trap

This is where copied rules fail. The Nginx location documentation says Nginx first picks the longest matching prefix location, then checks regular expressions “in the order of their appearance in the configuration file,” and “the search of regular expressions terminates on the first match.”

Most WordPress setups already have a generic block such as location ~ \.php$ that passes PHP to PHP-FPM. If that block appears above yours, it matches first and your deny rule never runs.

There are two fixes. The simplest is to place the uploads rule above the generic PHP block. A sturdier option uses the ^~ modifier, which the docs say makes Nginx skip regular expression checks when it is on the longest matching prefix. The return 403; directive is documented in the Nginx rewrite module as valid in server, location and if.

Test first: Same warning as above. Back up the config, use a staging copy, and verify with a .php request in uploads before trusting it. If your site uses a fastcgi_pass setup that differs from the usual one, the nested block may need changes.

location ^~ /wp-content/uploads/ {
    location ~* \.(?:php[0-9]*|phtml|phar|pht)$ {
        return 403;
    }
}

Here the outer block catches everything under uploads before the generic PHP rule, and the inner block refuses PHP-type files. Normal images are still served as static files.

After editing, run nginx -t. The Nginx command-line docs describe -t as checking the configuration syntax and trying to open the files it refers to. Then reload with nginx -s reload. Nginx’s beginner’s guide says that on reload it checks the new configuration and, if it is invalid, keeps running the old one. Keep a copy of the old file so you can restore it in seconds.

Method 3: Block PHP execution in WordPress uploads on LiteSpeed

LiteSpeed comes in two forms, and they behave differently.

LiteSpeed Enterprise is widely described as a drop-in Apache replacement that reads Apache-style .htaccess files. We could not open an official LiteSpeed page that lists which directives it supports, so we cannot confirm that FilesMatch and Require work on your server. Try the Apache rule from Method 1 on a staging copy and run the test below. Check with your host, because hosts can restrict directives.

Test first: Back up your .htaccess, try the rule on staging, then request a .php file inside wp-content/uploads and confirm 403 or 404. If you still see the file’s output, the rule is not active.

OpenLiteSpeed only supports rewrite rules from .htaccess. The OpenLiteSpeed documentation says unsupported directives are ignored, so the FilesMatch block above would do nothing. You also need “Auto Load from .htaccess” turned on for the virtual host, and the docs say you must restart OpenLiteSpeed after changing rewrite rules.

Use a rewrite rule instead. In the .htaccess file or the virtual host’s Rewrite settings, add:

Test first: This rule is untested on a live OpenLiteSpeed server. The docs we opened confirm rewrite-rule support, but they do not explain the F flag. Back up your configuration, try it on staging, restart OpenLiteSpeed, then request a .php file in uploads and confirm 403 or 404.

RewriteEngine On
RewriteRule ^wp-content/uploads/.*\.(php[0-9]*|phtml|phar|pht)$ - [F,NC,L]

In Apache rewrite syntax, the F flag returns 403 Forbidden and NC makes the match case-insensitive. We did not find the F flag documented on the OpenLiteSpeed page, so rely on your test result. The OpenLiteSpeed docs note that patterns set in the virtual host match the full path, so there you may need a leading slash, such as ^/wp-content/uploads/. Put the rule before WordPress’s own rewrite block if you add it to the root .htaccess.

Test that you can block PHP execution in WordPress uploads

Do not assume you managed to block PHP execution in WordPress uploads. Prove it, with a file that cannot hurt you.

  1. Create a plain text file on your computer named php-test.php with one line: <?php echo "PHP ran"; ?>
  2. Upload it with SFTP or your host’s file manager into wp-content/uploads/. Do not use the Media Library, which rejects PHP files.
  3. Open https://yoursite.com/wp-content/uploads/php-test.php in a browser, or run curl -i against it.
  4. Read the result.
What you seeMeaning
403 Forbidden or 404 Not FoundThe rule works
The words “PHP ran”PHP still executes there; the rule is not active
500 Internal Server ErrorA rule or config problem; undo it now
  1. Delete the test file when you are done. Do not leave any PHP file in uploads.

Also load a few pages and images afterward to confirm normal uploads still display.

What can break when you block PHP execution in WordPress uploads

Most sites run fine once you block PHP execution in WordPress uploads. A few things to check:

  • Plugins that write PHP into uploads. Some caching, backup, form or import plugins create files in the uploads folder. If a feature stops working after the change, look in the plugin’s settings for where it stores files.
  • Old themes or custom code that load PHP from uploads. This is unusual and usually a sign the code should move.
  • Server errors. A mistake in the pattern or a blocked directive can cause a 500 error. Remove the rule and the site returns to normal.
  • A false sense of safety. The rule does not remove a malicious file that is already there. If you suspect one, see WordPress malware removal.

If something breaks, delete the .htaccess file you created (Apache and LiteSpeed Enterprise) or restore your saved Nginx config and reload. Because you made a backup, undoing the change is quick.

What blocking PHP execution in WordPress uploads does not do

To block PHP execution in WordPress uploads is one layer, not a full defense. It does not:

  • Stop an attacker uploading files in the first place.
  • Fix an outdated or vulnerable plugin.
  • Block PHP in other writable folders, such as caches or temporary folders, unless you add rules there too.
  • Remove malware that is already installed in other places.

Pair it with the basics. Our WordPress security checklist covers updates, strong logins, backups and monitoring, and regular WordPress care plans keep that routine going after the first setup. WordPress’s hardening guide also recommends directories at 755, files at 644 and a wp-config.php readable only by you and the server, typically 400 or 440. And disabling the dashboard file editor with define( 'DISALLOW_FILE_EDIT', true ); removes another easy path.

If the site was already hacked, work through what to do when your WordPress site is hacked before you rely on any hardening.

Checklist to block PHP execution in WordPress uploads

  • [ ] Full site backup taken and the current server config copied
  • [ ] Web server identified (Apache, Nginx, LiteSpeed Enterprise or OpenLiteSpeed)
  • [ ] Rule added in the correct place for that server
  • [ ] Nginx location order checked, or config tested and reloaded
  • [ ] Harmless php-test.php returns 403 or 404
  • [ ] Test file deleted
  • [ ] Images, uploads and key plugin features still work
  • [ ] File permissions checked (directories 755, files 644)
  • [ ] Plugins, themes and core updated
  • [ ] Rule noted in your site documentation

Want us to check this on your server?

Server rules are easy to get wrong, and it is hard to know whether they are working. If you would rather not edit config files on a live site, we are happy to take a look. We can check which web server you run, add and test the rule safely, and tell you what else looks exposed. Our WordPress support and maintenance plans cover ongoing security checks and backups, and you can also ask for a one-off review through our contact page. If you simply need a developer to set it up, our WordPress development team can do that.

For ongoing care, our plans are Essential at $30 (PKR 8,400), Standard at $55 (PKR 15,400) and Pro at $100 (PKR 28,000) per month.

FAQ

Why block PHP execution in WordPress uploads?

The uploads folder is writable and holds files from many sources. If an attacker places a PHP file there, running it can give them control of the site. Refusing to run PHP in that folder makes that common attack far less useful.

Will blocking PHP in uploads break my plugins?

For most sites, no. Plugins normally store images, documents and generated CSS there, not PHP. If one stores executable PHP in uploads, a feature may stop working, so test key features after the change and check that plugin’s settings.

Does the .htaccess rule work on Nginx?

No. Nginx ignores .htaccess files. You need a location block in the Nginx config, and its order matters because the first matching regular expression wins.

How do I know the rule is working?

Upload a harmless php-test.php file to the uploads folder over SFTP and open its URL. A 403 or 404 means PHP is blocked. If you see the file’s output, the rule is not active. Delete the test file afterward.

Is it enough to block PHP execution in WordPress uploads?

No. It is one layer. You still need updates, strong passwords, correct file permissions, backups and monitoring. It also does not remove files an attacker already planted.

Should I use a security plugin instead?

Some security plugins add this rule for you, which can be easier. Check which web server they support, and still run the test above, because a rule that is written but not active protects nothing.