How to change php version in cPanel using .htaccess?

Castor 4 min read August 22, 2023 [ServerManagement] #PHP #cPanel #htaccess #WebHosting

Discover how to effortlessly update your PHP version using the .htaccess file in cPanel. Enhance your website's security, performance, and compatibility with this step-by-step guide to staying up-to-date in the dynamic world of web development.

How to change PHP version using .htaccess file?

Understanding .htaccess

The .htaccess (hypertext access) file is a powerful configuration file that allows you to modify various aspects of your web server's behavior for a specific directory or website. It's a handy tool for making changes without directly accessing the server's main configuration files.

Checking Available PHP Versions

Before you proceed, it's essential to verify the available PHP versions on your hosting server. Different hosting providers might offer different versions. You can typically find this information in your cPanel dashboard or by contacting your hosting support.

Steps to Change PHP Version

Log in to your cPanel account provided by your web host. This is usually done by visiting https://yourdomain.com/cpanel.

In cPanel, find and click on the "File Manager" icon. This tool allows you to manage files and directories on your server.

Once in the File Manager, make sure you're in the corrct directory of your website in which website you want to make the changes to. This is typically the public_html folder or the website's named folder.

The .htaccess file is often hidden by default. To make it visible, find and click on the "Settings" or "Options" button in the File Manager. Then, check the option to show hidden files.

Show Hidden Files in cPanle.

Locate the .htaccess file in the root directory and right-click on it. Choose the "Edit" option. If the file doesn't exist, you can create a new one using any text editor.

Edit htaccess file in cPanle.

To switch to a different PHP version, you need to add the appropriate code to your .htaccess file.

PHP 5.4

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php54 .php .php5 .phtml
</IfModule>

PHP 5.5

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php55 .php .php5 .phtml
</IfModule>

PHP 5.6

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php56 .php .php5 .phtml
</IfModule>

PHP 7.0

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php70 .php .php7 .phtml
</IfModule>

PHP 7.1

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php71 .php .php7 .phtml
</IfModule>

PHP 7.2

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php72 .php .php7 .phtml
</IfModule>

PHP 7.3

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php73 .php .php7 .phtml
</IfModule>

PHP 7.4

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php74 .php .php7 .phtml
</IfModule>

PHP 8.0

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php80 .php .php8 .phtml
</IfModule>

PHP 8.1

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php81 .php .php8 .phtml
</IfModule>

PHP 8.2

<IfModule mime_module>
  AddHandler application/x-httpd-alt-php82 .php .php8 .phtml
</IfModule>

After adding the code, save the changes to the .htaccess file.

Save htaccess file in cPanle.

Important Note:
Make sure that the php version is not being controlled from MultiPHP Manager or PHP Selector. Else you'll need to update the PHP Version from them as well.
Click Here to change the php version from MultiPHP Manager.
Click Here to change the php version from PHP Selector.

<?php phpinfo(); ?>

Access this file in your browser (e.g., https://yourdomain.com/info.php) and check the PHP version displayed.

Important Considerations

While changing PHP versions using .htaccess is convenient, it only affects the specific directory where the .htaccess file is located. Keep in mind that some PHP versions might have deprecated features or compatibility issues with your existing code. Make sure to test your website thoroughly after the version change. Regularly update your website's code to ensure it's compatible with newer PHP versions. Staying up-to-date will help you avoid security vulnerabilities and benefit from performance improvements.

Conclusion

Changing the PHP version using the .htaccess file in cPanel is a useful technique to manage your website's compatibility and performance. With a few simple steps, you can adapt your website to different PHP versions without directly modifying server configurations. Just remember to verify your changes and test your website thoroughly to ensure everything functions as expected.

Chat on WhatsApp!