How to change php version using htaccess

  1. HowTo
  2. PHP Howtos
  3. Change PHP Version Using .htaccess File

Created: February-25, 2022

  1. Use the .htaccess Files to Change Version of PHP
  2. Use the phpinfo[] to Check Our Version in PHP

We will look at how to change the PHP version using .htaccess files.

Use the .htaccess Files to Change Version of PHP

We will add the line below to our .htaccess file to specify the php version we want to use, in this case, php 7.3.

 

Output :



    SetHandler application/x-lsphp73

Use the phpinfo[] to Check Our Version in PHP


Output:

This article was last updated on January 20th, 2016

On the Crucial Hosting managed platform, we offer many different versions of PHP. You are able to choose which version you want to use at any time, since all of the PHP versions are available on all of our servers. As long as you have a Split-Shared, Split-Dedicated, or Managed Dedicated Server, then you can use the information below.

Changing your PHP version for web requests

The best way to change the version of PHP used for requests to your website is to add one of the following lines to your .htaccess file. One major convenience of this method is that you can change the PHP version for individual directories.

PHP 7.3

AddHandler application/x-httpd-php73 .php 

PHP 7.2

AddHandler application/x-httpd-php72 .php 

PHP 7.1

AddHandler application/x-httpd-php71 .php 

PHP 7.0

AddHandler application/x-httpd-php70 .php 

PHP 5.6

AddHandler application/x-httpd-php56 .php 

PHP 5.5

AddHandler application/x-httpd-php55 .php 

PHP 5.4

AddHandler application/x-httpd-php54 .php 

PHP 5.3

AddHandler application/x-httpd-php53 .php 

The default version of PHP for web requests

With very few exceptions, the default version of PHP on our servers is PHP 5.3. This is because many customers still run software which hasn't been updated to support anything more recent.

On our Split-Dedicated or Managed Dedicated Servers, you can change the default version of PHP in cPanel. We've created a custom tool to allow you to make this change. To do this, go to "PHP Config" in cPanel, and then Select the Version of PHP you prefer. Once you've picked the preferred PHP version, select "Set as Default PHP".

Making sure you are on the right version of PHP

The easiest way to do this is to simply make a "PHP Info" file. It's simple to do. Just make a file and name it something like myphpinfo.php, or anything that ends with .php. Then just simply place the following line of code in the empty file.

Once you've created and saved that file, you need to view it in your browser. Once you view the file in your browser, you'll see the PHP version at the top of the page.

An advanced approach to PHP versions and .htaccess regular expressions

For more advanced web applications, you may find yourself in a situation where multiple scripts in the same directory require different PHP versions. In that situation, you can use the "Files" or "FilesMatch" directive in your .htaccess file. The example below looks for URI requests that contain "mynewscript", and uses PHP 5.5. In the event the URI contains "mynewerscript", it uses PHP 5.6. If neither of these are matched, it uses the default PHP version [or whatever is set above this in this in the .htaccess file.]


AddHandler application/x-httpd-php55 .php



AddHandler application/x-httpd-php56 .php

Picking a PHP version for command line and cron scripts

The way you would typically call a PHP script is by starting your command with "php". As an example, the following line will let you see the default version of PHP on your server [for command line only].

php -v

Now many scripts may require you to run a different or more recent version of PHP. That's easy to do. As an example, you can replace "php" with "php-54" to request PHP 5.4. Below are some examples of how to call the various versions of PHP.

Default PHP Binary

php -v

PHP 5.4

php-54 -v

PHP 5.5

php-55 -v

PHP 5.6

php-56 -v

PHP 7.0

php-70 -v

PHP 7.1

php-71 -v

PHP 7.2

php-72 -v

PHP 7.3

php-73 -v

The initial path to all PHP versions is the same.

/usr/local/bin/

When you need the full path to the PHP version of your choice [cron, scripts, etc] simply use the the path above with the correct PHP binary. Example: For PHP version 5.5 you would use /usr/local/bin/php-55.

Changing the version of PHP for the default "php" binary

If you are on a Split-Dedicated or Managed Dedicated Server, we can change your default PHP version for the command line. Please understand that this will affect every PHP request on your entire server. You can create a ticket in our support center, and we'll make this change for you.

Can I change PHP version in htaccess?

htaccess file, right-click on it, and select edit. At the top of the file, insert the PHP handler for the version you would like to use. If there is a handler in the file already, it should be removed or replaced with your new handler.

How do I change my PHP version?

Changing the PHP version.
Navigate to the Manage Websites page..
Click the Manage button to the right of your domain..
Under the PHP section, click the Modify icon..
Choose a version of PHP from the dropdown menu..
Scroll down and click the Change PHP Version button to save your changes..

How can I downgrade PHP version in PHP?

ini settings file, so you'll need to reconfigure PHP upon downgrading if you have made any customizations or tweaks to its settings..
Visit windows.php.net in your browser, and click "Downloads.".
Select the version you'd like to install from the drop-down "Binaries and. ... .
Click the link labeled "Zip" from the download page..

Can I update PHP version in cPanel?

Change the PHP Version on Your Sites Click the MultiPHP Manager link in the Software section of cPanel. Check the box for the site or sites you wish to update. Select the desired PHP Version from the drop-down menu. Click the Apply button.

Bài mới nhất

Chủ Đề