Chapter 14 · 14.5
PHP settings per directory and via .htaccess
Sometimes you want one folder to work differently from the rest of your website. For example, an old web shop in a subfolder that can't handle the newer PHP version yet, or a single script that needs more memory. You can arrange this with a file called .htaccess in that folder. Our servers run LiteSpeed, and LiteSpeed reads PHP rules in .htaccess.
Please note: A mistake in .htaccess can make your entire website (or the folder) unreachable with a 500 error. Make a copy of the file first and test it immediately after saving. You can read how to find and edit .htaccess in Showing and editing hidden files such as .htaccess.
Changing a PHP setting for one folder
Add lines like these to the .htaccess file in that folder:
php_value upload_max_filesize 128M
php_value post_max_size 128M
php_value memory_limit 384M
php_value max_execution_time 300
For settings that are either on or off, use php_flag, for example php_flag display_errors off.
The rules apply to that folder and all folders beneath it. If you add them to the .htaccess file in public_html, they'll apply to your whole website.
Tip: Want to change a setting for your whole package? It's better to do this in Select PHP version (see PHP options: upload_max_filesize, memory_limit and max_execution_time). That way, everything's in one place and you won't forget later that there are still rules in .htaccess.
A different PHP version for one folder
Your whole package uses the version you've chosen in Select PHP version. If you want to give one folder (or a subdomain, which also has its own folder) a different version, add this to the .htaccess file in that folder:
<FilesMatch "\.(php)$">
SetHandler application/x-lsphp83
</FilesMatch>
The number after lsphp is the version without the dot: 74 for PHP 7.4, 82 for PHP 8.2, 83 for PHP 8.3, 84 for PHP 8.4, and so on. You can choose from the same versions as in Select PHP version.
Are you using extensions or settings in that folder that differ from the rest? These belong to the version. In that case, temporarily select that version in Select PHP version, set the extensions and settings, and then switch the version for your package back. The settings for each version are saved separately.
Checking that it works
- Create a file called info-test.php in the folder with this single line: <?php echo PHP_VERSION, ' ', ini_get('memory_limit');
- Open it in your browser, for example https://www.yourdomain.com/oldshop/info-test.php. You'll see the PHP version and the memory limit.
- Delete the file straight afterwards. Don't leave test files containing information about your server on your website.
What doesn't work?
- A custom php.ini in your folder has no effect. Use Select PHP version or .htaccess instead.
- disable_functions and open_basedir can't be changed via .htaccess. These are part of the server's security.
- Values set as an override for the domain in PHP Settings (see PHP Settings in DirectAdmin or Select PHP version: what's the difference?) take priority over .htaccess.
