How to change PHP version in htaccess - cPanel

Setting custom PHP version per directory

The PHP version is crucial for many reasons than just performance and speed. various applications may call for various versions of PHP. All shared hosting customers may use FastComet to specify multiple PHP versions for certain folders. To run PHP 7.4 on your account, for instance, but your application requires PHP 7.2, you may accomplish that by creating custom rules for every folder on your account. We'll go over how to run several PHP versions on the same account in this post.

Choosing PHP version for a specific directory of your account

Any combination of several PHP versions that are active at the same time in any directory on the account is allowed. Even though the PHP Selector UI does not support this, doing it by hand is not too difficult.

You must add a specific code to your .htaccess file in order to establish a PHP version that differs from the one that is given to your domain on your cPanel account. A hidden file that is read by the webserver is the.htaccess file. Whatever instructions are in the file are followed by the web server. We'll go over how to access it using the File Manager in the cPanel of your hosting account for the sake of this tutorial.

Go to the Files → File Manager section. Choose the public_html folder and modify the .htaccess file for your primary domain there. To make modifications to an addon domain, navigate to the public_html/youraddondomain.com folder.

Make sure Show hidden files is activated while you're in the File Manager:


Show Hiden Files in cPanel

In this manner, "dot" files—which are often hidden by default—will be visible to you. In that scenario, the .htaccess file is the particular file you require.

Either pick the .htaccess file and select Edit from the toolbar, or right-click over it and select Edit the file to define a custom PHP version for the directory of your choice:


Edit htaccess file in cPanel

You'll see a pop-up window where you need to click Edit once more:

PHP Handlers Available

Insert the PHP handler for the version you want to use at the top of the .htaccess file now that you are inside of it. Should a handler already exist in the file, it need to be eliminated or substituted with your newly created handler. Add one of the following lines, depending on the PHP version you want for your particular application (folder):

For PHP 5.6:
AddHandler application/x-httpd-php56 .php .php5

For PHP 7.0:
AddHandler application/x-httpd-php70 .php .php5

For PHP 7.1:
AddHandler application/x-httpd-php71 .php .php5

For PHP 7.2:
AddHandler application/x-httpd-php72 .php .php5

For PHP 7.3:
AddHandler application/x-httpd-php73 .php .php5

For PHP 7.4:

AddHandler application/x-httpd-php74 .php .php5

For PHP 8.0:
AddHandler application/x-httpd-php80 .php .php5

For PHP 8.1:
AddHandler application/x-httpd-php81 .php .php5

For PHP 8.2:
AddHandler application/x-httpd-php82 .php .php5

For customers using our CooliceHost cPanel packages, please use the following snippet in your .htaccess file:

< FilesMatch "\.(php4|php5|php3|php2|php|phtml)$" >
SetHandler application/x-lsphp71
< /FilesMatch >

Note: remove the spaces after and before "<" and ">"!

The actual change is made by switching the SetHandler line with one of these depending on the version (look at the last two digits):

SetHandler application/x-lsphp56
SetHandler application/x-lsphp70
SetHandler application/x-lsphp71
SetHandler application/x-lsphp72
SetHandler application/x-lsphp73
SetHandler application/x-lsphp74
SetHandler application/x-lsphp80
SetHandler application/x-lsphp81
SetHandler application/x-lsphp82

After you have added your own configuration rules, save your changes and close the .htaccess file. Now you should have your custom PHP version set.

If you have difficulty with the PHP settings of your hosting account, do not hesitate to contact the CooliceHost technical support team.


Was this article helpful?

mood_bad Dislike 0
mood Like 3
visibility Views: 562