View Categories

How to Set Different PHP Versions for Each Domain and Subdomain using .htaccess

This guide shows you how to use different PHP versions for your main domain, addon domains, and subdomains within the same hosting account. This is useful when you have multiple websites with different PHP requirements.

What You Can Achieve

  • Main domain: yourdomain.com can run PHP 8.2
  • Addon domain: anotherdomain.com can run PHP 8.1
  • Subdomain: shop.yourdomain.com can run PHP 7.4
  • Different directories: Each can have its own PHP version

Directory Structure

Your hosting account should have something of this structure:

				
					/public_html/                           # Main domain files
/public_html/addon-domain.com/          # Addon domain files
/public_html/subdomain/                 # Subdomain files
				
			

Method : Create .htaccess Files #

Create or edit the .htaccess file in each domain directory with the appropriate PHP version code.

Here is the .htaccess Code for Each PHP Versionย 

For PHP 7.4:

๐Ÿ“„
.htaccess
<IfModule mod_mime.c>
    AddType application/x-httpd-lsphp74 .php
</IfModule>

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

For PHP 8.0:

๐Ÿ“„
.htaccess
<IfModule mod_mime.c>
    AddType application/x-httpd-lsphp80 .php
</IfModule>

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

For PHP 8.1:

๐Ÿ“„
.htaccess
<IfModule mod_mime.c>
    AddType application/x-httpd-lsphp81 .php
</IfModule>

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

For PHP 8.2:

๐Ÿ“„
.htaccess
<IfModule mod_mime.c>
    AddType application/x-httpd-lsphp82 .php
</IfModule>

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

For PHP 8.3:

๐Ÿ“„
.htaccess
<IfModule mod_mime.c>
    AddType application/x-httpd-lsphp83 .php
</IfModule>

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

For PHP 8.4:

๐Ÿ“„
.htaccess
<IfModule mod_mime.c>
    AddType application/x-httpd-lsphp84 .php
</IfModule>

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

Use case scenarios :ย  #

Example Setup:

Main Domain (PHP 8.2):

  • Create/edit: /public_html/.htaccess
  • Add the PHP 8.2 code above

Addon Domain (PHP 8.1):

  • Create/edit: /public_html/addon-domain.com/.htaccess
  • Add the PHP 8.1 code above

Subdomain (PHP 7.4):

  • Create/edit: /public_html/subdomain/.htaccess
  • Add the PHP 7.4 code above

Common Use Cases

WordPress Sites with Different Requirements

Scenario: You have multiple WordPress sites with different plugin requirements:

  • Main site: Modern theme requiring PHP 8.2
  • Old blog: Legacy plugins requiring PHP 7.4
  • E-commerce: Payment gateway requiring PHP 8.1

Solution: Set different PHP versions for each domain directory.

Development and Production Environments

Scenario: You want to test your site with newer PHP versions:

  • Production: yourdomain.com stays on stable PHP 8.1
  • Testing: test.yourdomain.com uses PHP 8.3 for testing
  • Legacy: old.yourdomain.com uses PHP 7.4 for old applications

Mixed Application Requirements

Scenario: You host different types of applications:

  • WordPress blog: PHP 8.2 for performance
  • Old custom CMS: PHP 7.4 for compatibility
  • New Laravel app: PHP 8.3 for latest features

Troubleshooting #


PHP Version Not Changing

  1. Clear browser cache and refresh the page
  2. Check .htaccess syntax – ensure there are no typos
  3. Verify file permissions – .htaccess should be readable
  4. Wait a few minutes – changes may take time to propagate

Website Shows Error After Changing PHP Version

  1. Check error logs in your control panel
  2. Common issues:

    Script requires newer/older PHP version
    Missing PHP extensions
    Memory limit too low

Memory Limit Issues

If you get memory errors, add this to your .htaccess file:

๐Ÿ“„
.htaccess
# Add this BEFORE the PHP version settings
php_value memory_limit 256M
php_value max_execution_time 300

# Then add your PHP version code here
<IfModule mod_mime.c>
    AddType application/x-httpd-lsphp82 .php
</IfModule>

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

Checking your current PHP versionย  #

Create a file called phpinfo.php with this content:

๐Ÿ˜
phpinfo.php
<?php
phpinfo();
?>

Upload it to any directory and visit it in your browser. It will show:

  • Current PHP version
  • Available extensions
  • Configuration settings

Remember to delete this file after checking as it shows server information.