How to change the PHP version for a specific directory

Setting the PHP version for a specific directory is a useful technique for web developers and administrators who require flexibility in managing different applications within the same hosting account. While cPanel’s PHP Selector typically sets the PHP version for the entire account, there are scenarios where users may need to utilize a different PHP version for a specific directory.

Changing the PHP version for a directory

To change the PHP version for a specific directory, follow these steps:

  1. Using your preferred text editor, create a file named .htaccess in the directory where you want to use a different PHP version. If the directory already has the .htaccess, back the file up and edit and then reupload or better still edit using the cPanel editor tool
  2. Copy the following text below:
    <FilesMatch ".(php4|php5|php3|php2|php|phtml)$">
        SetHandler application/x-httpd-alt-phpXX___lsphp
    </FilesMatch>
  3. Replace XX with the PHP version you want to use in the directory. You can use any PHP version that is listed in the cPanel PHP Selector. For example, to enable PHP 8.1, you would type SetHandler application/x-httpd-alt-php81___lsphp. (Make sure you do not include the period in the version number. For example, for PHP version 7.4, rather type 74.)
  4. Paste the modified text into the .htaccess file.
  5. Save the .htaccess file. The new PHP version takes effect immediately.

By following a few simple steps, you can simply override the default PHP version and specify a different version for a particular directory. This allows for greater customization and compatibility with various applications running on the same server.