☰
WORDPRESS / PRACTICE

How to Disable PHP Execution in wp-content/uploads

How to Disable PHP Execution in wp-content/uploads

The `wp-content/uploads` directory is intended for images, documents, videos, and other uploaded media files. WordPress normally should not execute PHP code from this location. If an attacker exploits a vulnerable plugin, theme, or file-upload mechanism, a webshell—a PHP file that can execute commands on the server—could be placed in this directory.

A server-side restriction on PHP execution in `uploads` does not eliminate the underlying vulnerability, but it limits the consequences of a compromise. Below are configurations for Nginx and Apache, a testing procedure, and common mistakes that can prevent the rule from working.

Why wp-content/uploads Requires Separate Protection

Media files in `wp-content/uploads` need to remain readable by the web server, but PHP files in this directory are generally unnecessary for the site to function. If a file with a `.php`, `.phtml`, or `.phar` extension is passed to PHP-FPM or another PHP handler, it may execute malicious code with the privileges of the web server user.

Cases where a file is disguised as an image or uses a double extension are especially dangerous: `image.php`, `image.phtml`, and `photo.jpg.php`. WordPress MIME-type and extension checks are useful, but they should not be the only protection. The web server should make the final decision.

  • Images and documents continue to open as static files.
  • PHP files in `uploads` are not passed to the interpreter.
  • The restriction remains effective even if a file appears as a result of an attack against a plugin or another component.

Disable PHP Execution in uploads on Nginx

For Nginx, add the rule to the configuration of the relevant virtual host or another configuration level that handles the site's requests. It blocks requests to PHP files in `wp-content/uploads` and its subdirectories, regardless of the extension's letter case.

  • Make sure the rule belongs to the correct `server` block.
  • After editing, test the configuration syntax first.
  • Then reload or otherwise correctly apply the Nginx configuration.
location ~* ^/wp-content/uploads/.*\.(php|phtml|phar)$ {
    deny all;
}

What to Check in the Nginx Configuration

In complex Nginx configurations, other regular-expression `location` blocks or PHP rules may also exist. It is important to verify which block actually handles a request for a file in `uploads`. Otherwise, a general PHP rule could process the request first, or the configuration could be active in a different virtual host.

Do not limit the rule to the root of the `uploads` directory: the `.*` pattern must also cover nested directories. After deployment, check Nginx's effective configuration and access and error logs.

  • The configuration syntax is commonly tested with `nginx -t` using administrator privileges.
  • Test URLs with uppercase letters in the extension, such as `test.PHP`.
  • If the site uses a nonstandard prefix or a separate domain for media, adapt the rule to the actual URL path.

Disable PHP Execution in uploads on Apache

For Apache, the rule can be placed in a `.htaccess` file directly inside `wp-content/uploads`. It denies access to files with the specified extensions, including files in subdirectories when those directories inherit Apache's rules.

  • Create or edit the `wp-content/uploads/.htaccess` file.
  • Make sure Apache permits the use of `.htaccess` files for this directory.
  • After making the change, test access to a normal media file and a test PHP file.
<FilesMatch "\.(php|phtml|phar)$">
    Require all denied
</FilesMatch>

What to Do If .htaccess Does Not Work

The presence of an `.htaccess` file does not guarantee that Apache reads it. The `AllowOverride None` directive disables the application of local rules. The server may also lack the required modules, or the site may operate behind Nginx as a proxy, meaning that Apache changes will not affect the external request.

Check the virtual host configuration, Apache logs, and the actual web-server chain. Do not replace the modern `Require all denied` directive with outdated rules unnecessarily: the syntax must match the Apache version and configuration.

  • Check whether `AllowOverride` is permitted for the uploads directory.
  • Determine which server receives the HTTPS request first.
  • After making changes, clear only the necessary cache if a reverse proxy or CDN is in front of the site.

How to Verify the Block

Testing should confirm two things: media files remain accessible, and PHP is not executed from the directory. Do not use real malicious code for the test. A temporary file that simply outputs text is sufficient.

  • Upload or open a normal image from `uploads`. It should be served without an error.
  • Create a file such as `security-test.php` in `uploads` containing a simple `echo` statement, then open it in a browser or HTTP client.
  • The expected result for the PHP file is `403` or `404`, not a page containing the output of executed PHP.
  • Repeat the test in a nested directory and for `.phtml`, `.phar`, and uppercase extension variants.
  • Delete the test file immediately after testing.
<?php echo 'uploads security test';

Testing Through HTTP and Logs

Test the actual external URL, not just access to the file through the local filesystem. For example, an HTTP client should receive a blocking status, and the relevant request should appear in the web server logs. If the browser displays the output of the test PHP file, the rule is not working, or the request is being handled by another server or route.

A `403` status clearly indicates that access was denied. A `404` can also be an acceptable result if the server intentionally hides the existence of such files. The important point is that the code must not execute.

  • Check the response without caching if a CDN is in front of the site.
  • Compare the Nginx or Apache logs with the PHP-FPM logs.
  • Do not consider the test successful merely because the file cannot be uploaded through the WordPress media library.

Subdirectories, Symlinks, and Deployment Considerations

The protection must cover the entire physical `uploads` directory, not just its current level. Check year-and-month folders, custom subdirectories created by media plugins, and directories generated during imports or migrations.

Pay particular attention to symlinks, a nonstandard `DocumentRoot`, alias rules, and domains used for static files. A URL may look like a path to uploads while actually being handled by a different `location` block or virtual host. In that case, the rule must be applied at the actual routing level.

  • Check `.htaccess` permissions and whether the configuration remains available after restoring a backup.
  • Do not copy an old configuration file to a new server without checking its structure.
  • Repeat a brief blocking test after every deployment.

Common Mistakes and Additional Measures

The most common mistake is relying only on WordPress or MIME-type validation. An attacker may use another upload endpoint, exploit a plugin vulnerability, or already have access to the filesystem. A server-side restriction is a separate layer of protection, but it does not replace updating WordPress, themes, and plugins.

You should also limit write permissions where possible, remove unknown files, check logs for suspicious requests, and create regular backups. If PHP files have already been found in `uploads`, do not simply delete them: inspect the site for signs of compromise and investigate how the files appeared.

  • Update WordPress, plugins, and themes from trusted sources.
  • Remove unused components instead of merely deactivating them.
  • Monitor the appearance of new files in `wp-content/uploads`.
  • Review logs after installing or updating the rules.
  • Store backups separately from the web server.

Frequently asked questions

Can execution of all files in wp-content/uploads be disabled completely?

Yes. For a typical WordPress site, this is the correct model: uploads should serve static files, not execute PHP. Exceptions may exist for specialized components that intentionally use server-side logic in this directory; such cases must be reviewed separately.

Which response is better: 403 or 404?

Both are acceptable if PHP is not executed. `403` explicitly indicates that access is forbidden, while `404` can also hide the file's existence. The choice depends on the server's configuration policy.

Why did the Nginx rule not work even though it was added to the configuration?

Possible causes include the wrong virtual host, another `location` route, a CDN, a nonstandard uploads path, or a configuration that was not applied after editing. Check the active configuration, logs, and the actual HTTP request.

Is disabling PHP execution enough to protect uploads?

No. This is an important hardening layer, but it does not eliminate file-upload vulnerabilities, stolen credentials, or infections in other directories. Updates, access control, monitoring, and backups are also required.

Conclusion

Disabling PHP execution in `wp-content/uploads` is a simple server-side measure that reduces the risk of a downloaded file being turned into a webshell. Configure the rule on Nginx or Apache, make sure it covers subdirectories, and then test the actual HTTP request and logs.

After deployment, confirm that images and documents work while a test PHP file returns `403` or `404`. Combine this restriction with updates, minimal access permissions, and monitoring for newly created files.

← All WordPress materials