Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

.htaccess Rules to Prevent PHP Execution

2 min read .
.htaccess Rules to Prevent PHP Execution

Public upload directories are a common security-sensitive part of web applications. If an attacker manages to upload a file such as shell.php and the web server executes it, the upload feature can become a route to remote code execution.

On Apache, a directory-specific .htaccess configuration can help prevent script execution in locations that should contain only static files.

Why .htaccess Can Help

Apache supports .htaccess files for directory-level configuration when the server permits the relevant overrides. This makes it possible to apply security rules to a specific directory without changing every virtual-host setting.

Directories such as /uploads, /assets, and /media often need to serve images, PDFs, and other static files, but they generally do not need to execute PHP.

Example .htaccess Rules

php_flag engine off

<FilesMatch "\.(php|php[34578]?|phtml)$">
  RemoveHandler .php .php3 .php4 .php5 .php7 .php8 .phtml
  RemoveType .php .php3 .php4 .php5 .php7 .php8 .phtml
  ForceType text/plain
  Require all denied
</FilesMatch>

Options -ExecCGI
Options -Includes

Here is what each part does.

1. php_flag engine off

Where supported by the PHP/Apache configuration, this disables the PHP engine for the directory so PHP files are not executed there.

2. <FilesMatch "\.(php|php[34578]?|phtml)$">

This rule targets PHP-related file extensions:

  • RemoveHandler and RemoveType → remove configured PHP handlers and MIME type associations for those extensions.
  • ForceType text/plain → asks Apache to serve matching files as plain text if they are otherwise reachable.
  • Require all denied → denies HTTP access to matching files entirely.

The deny rule is the important final barrier: an uploaded shell.php should not be accessible through the web server.

3. Options -ExecCGI

Disables CGI execution in the directory, reducing the risk of uploaded CGI-compatible scripts being executed.

4. Options -Includes

Disables Server Side Includes (SSI), which are unnecessary in a static upload directory and can introduce additional execution behavior.

Put the Rules in the Upload Directory

A common location is:

/var/www/html/uploads/.htaccess

For frameworks with a public upload directory, it may look like:

/public/uploads/.htaccess

Static uploaded files remain available according to your other rules, while executable script types are blocked.

Optional: Allow Only Selected File Extensions

You can make the directory more restrictive by explicitly allowing a set of file extensions:

<FilesMatch "\.(jpg|jpeg|png|gif|pdf|docx|zip)$">
  Require all granted
</FilesMatch>

<FilesMatch ".*">
  Require all denied
</FilesMatch>

Test this carefully because rule ordering and Apache authorization merging can affect the final behavior. Also validate uploaded files by content, not only by filename extension.

Conclusion

Preventing script execution in public upload directories is an important defense-in-depth measure. Use server-level configuration when you control the server, and use .htaccess only when directory overrides are part of your deployment model. Combine these rules with strict upload validation, randomized storage names, safe permissions, and ideally storage outside the executable web root.

Related Posts

chevron-up