Apache – How to Change PHP Version via .htaccess

.htaccessapache-2.4httphandlerPHPphp-fpm

I've a Centos 7 with Apache, I want to compile php 7.0 and 5.6 and configure that as PHP-FPM.
How can I configure Apache to allow users to change php version using .htaccess?

Example .htaccess entry:

AddHandler application/x-httpd-php7 .php

Best Answer

If you plan to use mod_php, you can't, because the directive that takes care of loading the module can't be used in .htaccess context.

If your server is using mod_proxy and mod_proxy_fcgi to access a php-fpm server with your desired php version, because this method makes httpd completely "agnostic" on which php version you may be using, all you have to do is tell your server to point to the appropiate php-fpm unix socket for php files:

<FilesMatch \.php>
    SetHandler proxy:unix:/path/to/app.sock|fcgi://localhost/
</FilesMatch>