Disable PHP in directory (including all sub-directories) with .htaccess
If you are managing a multi-tenant application where specific user directories need to serve static content while preventing arbitrary code execution, you likely face the exact scenario described: a root structure containing PHP logic (index.php, control_panel.php) and a /USERS folder populated with individual subdirectories that must treat uploaded files as pure data.
The goal is straightforward but requires precision: you need to disable PHP processing for everything inside /USERS while keeping Server-Side Includes (SSI) active. This specific combinationâdisabling the PHP interpreter but retaining SSI parsingâis a nuanced configuration often required when serving dynamic static content or legacy templates without full server-side scripting capabilities.
The Core Configuration Logic
To achieve this, you must place an .htaccess file inside the /USERS directory. You cannot do this from the root; Apache directives applied to the main .htaccess will affect the entire site, which contradicts your need for per-directory control.
The directive responsible for controlling PHP execution is AddHandler. By default, Apache maps .php files to the PHP handler (application/x-httpd-php). To disable this mapping specifically for the /USERS tree, you add the following line to the .htaccess file located at ./USERS/.htaccess:
AddHandler .none .php
This tells Apache to ignore the standard PHP handler for any request targeting a file ending in .php within this directory. When Apache encounters a request like /USERS/DEMO1/script.php, it will not invoke the PHP engine, regardless of whether script.php contains valid syntax. Instead, the server treats the file as plain text or serves it with a generic MIME type depending on your root configuration, effectively preventing execution.
Enabling Server-Side Includes
The requirement to keep SSI active adds a layer of complexity because SSI is often handled by the same handler chain that PHP uses in some configurations, or requires its own specific flag. You must explicitly enable the SSI module before attempting to disable PHP. Add the following line immediately after your AddHandler directive:
AddType text/html .shtml
AddType text/plain .shtml
Options +Includes
SetEnvIfNoCase Request_URI "^/USERS" SSIParentPath /
However, a more robust approach for this specific "disable PHP but keep SSI" use case involves using the AddHandler directive to map .shtml (or your chosen extension) explicitly to the server-side includes handler while ensuring no other handlers are assigned. If you are relying on the default SSI behavior where files with .shtml extensions parse includes, you can simply ensure that option is toggled on:
Options +Includes
SetHandler ssi
Note: The SetHandler ssi directive overrides the content type handling for specific locations. If your uploaded files do not have an .shtml extension but still need SSI, you must map their current extension to the SSI handler instead of PHP.
Handling Sub-Directories Automatically
The prompt specifies "including all sub-directories." With Apache's AllowOverride and .htaccess behavior, a single .htaccess file placed in /USERS applies recursively to all nested folders (e.g., /USERS/DEMO1, /USERS/DEMO2). You do not need to duplicate these rules.
However, you must ensure that AllowOverride All is set in the main Apache configuration for the site root or specifically for the /USERS directory. If this is missing, Apache will ignore your .htaccess file entirely, rendering the PHP disabling ineffective. This is a common pitfall where the application works initially, but security hardening fails silently because the override permission was stripped during deployment.
Critical Pitfalls and Trade-offs
While disabling PHP prevents execution, it does not remove the files from disk. An attacker who can upload a file named exploit.php to /USERS/DEMO1 can still download it by requesting /USERS/DEMO1/exploit.php. The server will return the raw source code rather than executing it. If your use case involves serving these files to end-users, you must ensure that downloading .php files is not desirable behavior, or you should rename them before upload (e.g., exploit.txt).
There is also a distinction between "disabling execution" and "blocking access." The configuration above stops the PHP engine from running. If your /USERS directory is intended to be completely static, consider adding a rule to redirect .php requests to a 403 Forbidden page or a placeholder if you want to prevent users from even seeing the raw source code of uploaded scripts.
Finally, remember that this configuration relies on mod_rewrite and mod_include being loaded in your Apache environment (such as WAMP). If these modules are disabled globally, neither PHP disabling nor SSI enabling will function, regardless of the .htaccess content. Always verify module status before assuming the directives take effect.
