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, a single script that needs more memory or a higher upload limit. 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 is not possible
Your whole package uses the version you've chosen in Select PHP version (see Selecting the PHP version (Select PHP version)). A different version for one folder or subdomain, for example with a line SetHandler application/x-lsphp83 in .htaccess, doesn't work on our servers. If such a line is still in a .htaccess file, remove it.
Does one website really need to run on a different version than the rest? Then put that website in a separate hosting package. If an old website only works on an old version, have it updated (see Errors after a PHP upgrade and finding the error log).
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.
