How to Change PHP Version via .htaccess on Hostinger

Changing the PHP version of your website is usually done through the hosting control panel. However, for more advanced configurations—such as running a different PHP version in a specific subfolder—using the .htaccess file is the most effective method. This guide explains how to implement these changes on Hostinger’s platform.

The Power of Directory-Specific PHP Versions

The main advantage of using .htaccess over the global PHP Selector is localization. While the global setting affects your entire public_html directory, an .htaccess file allows you to:

  • Keep your main website on a modern version like PHP 8.2.
  • Run a legacy script in a subfolder (e.g., /old-site/) using PHP 7.4.
  • Test new code compatibility in a staging folder without affecting the live site.

Implementation Guide for Hostinger (hPanel)

Hostinger uses the LiteSpeed web server with CloudLinux. To change the PHP version, you need to define a specific "handler." Open your .htaccess file (or create a new one in the target folder) and add the appropriate code at the very top:

For PHP 8.2

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

For PHP 8.1

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

For PHP 7.4 (Legacy)

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


Important Safety Tips

  1. Avoid 500 Internal Server Errors: If your site breaks after saving the file, there is likely a typo in the handler name or a conflict with server settings. Simply remove the added lines to restore access.
  2. Dashboard Priority: Note that .htaccess rules generally take precedence over the settings selected in the hPanel "PHP Configuration" menu.
  3. Module Compatibility: When you change versions via .htaccess, ensure that any specific PHP extensions your code requires are enabled in the global PHP Selector, as the .htaccess only changes the version, not the active modules.

Conclusion

Using .htaccess provides the flexibility needed for complex hosting environments. It allows developers to maintain multiple applications with different requirements under a single Hostinger account seamlessly.

Copyright © 2025 Akhmad.dev