Setting PHP Versions Per Folder / Website Using .htaccess (AddHandler)
Knowledgebase Article
}
Knowledgebase Article
By default, your website runs on the PHP version set in cPanel. If you’re looking to set a different version of PHP account wide, take a look at how to do this using the Select PHP Version tool.
However, if your hosting account contains multiple websites, subdomains, or addon domains, you may need different PHP versions for different parts of your setup.
This is particularly useful when hosting a mix of modern and legacy applications that require specific PHP versions.
You can achieve this by adding an AddHandler rule to the .htaccess file for each website or folder where a different PHP version is needed.
There are several reasons you might want to manually set a PHP version for your website:
Note on Older PHP Versions
On our servers, older PHP versions are security-hardened, so even if they are no longer officially supported, they remain secure. However, using an older PHP version may indicate that your application is outdated. We strongly recommend ensuring that your software is up to date and follows best security practices.
The .htaccess file applies settings to the folder it resides in and all its subfolders. This means that if you add an AddHandler rule at the root of your website, it will apply to your entire site. If you only want to change the PHP version for a specific folder (e.g., an addon domain, subdomain, or a staging site), make sure to add the .htaccess file within the folder where your application resides.
To set a specific PHP version for your site or a folder, add the following line to your .htaccess file:
AddHandler application/x-httpd-phpXX .php // Replace XX with the desired PHP version
Ensure that you replace XX in the AddHandler directive with the required PHP version for your website or application.
For example, to use PHP 8.1:
AddHandler application/x-httpd-php81 .php // This sets PHP 8.1 for this directory and its subdirectories
More Examples:
If you want to set different PHP versions for different parts of your site, you can create separate .htaccess
files in the relevant folders. Each file applies only to the directory it is placed in and any subdirectories. Ensure you do not add these rules to a single .htaccess
file that tries to set multiple PHP versions for different directories.
Example 1: Setting PHP 8.0 globally
Add this to the .htaccess
file in your root directory (/public_html/
), applying it to the entire website:
AddHandler application/x-httpd-php80 .php // Sets PHP 8.0 for all scripts in this directory and subdirectories
Example 2: Setting PHP 5.6 for a legacy application
If you have an older application in the legacy folder that requires PHP 5.6, create a separate .htaccess
file inside /public_html/legacy/
and add:
AddHandler application/x-httpd-php56 .php // Sets PHP 5.6 for scripts within the legacy folder
Example 3: Setting PHP 8.2 for a staging environment
If you want to test your website with PHP 8.2 in a staging environment located in /public_html/staging/
, add the following to .htaccess
inside that folder:
AddHandler application/x-httpd-php82 .php // Sets PHP 8.2 only for the staging folder
These examples ensure that each section of your website runs on the required PHP version while preventing conflicts with other parts of your hosting environment.
Below is a list of available PHP versions you can use with the AddHandler directive at the time of writing:
AddHandler application/x-httpd-php56 .php // PHP 5.6
AddHandler application/x-httpd-php70 .php // PHP 7.0
AddHandler application/x-httpd-php71 .php // PHP 7.1
AddHandler application/x-httpd-php72 .php // PHP 7.2
AddHandler application/x-httpd-php73 .php // PHP 7.3
AddHandler application/x-httpd-php74 .php // PHP 7.4
AddHandler application/x-httpd-php80 .php // PHP 8.0
AddHandler application/x-httpd-php81 .php // PHP 8.1
AddHandler application/x-httpd-php82 .php // PHP 8.2
AddHandler application/x-httpd-php83 .php // PHP 8.3
To confirm that your website is using the correct PHP version:
php_version.php
) in your website’s root folder. We recommend using a custom name for this file of your own choosing.
<?php
phpinfo();
?>
To modify the .htaccess file, you can use one of the following methods:
/home/youruser/public_html/yourfolder
to navigate to the correct directory..htaccess
file using nano .htaccess
or vim .htaccess
.Note: If an .htaccess file does not exist, you can create a new file named .htaccess in the relevant directory using any of the above methods. Ensure the filename is exactly .htaccess without an extension.
AddType
, SetEnv
, or .user.ini
files, which may not be compatible on our environment. If you experience issues, review your .htaccess
file and replace any existing PHP version settings with the recommended AddHandler directive above to ensure proper functionality.Powered by WHMCompleteSolution