Skip to content

Instantly share code, notes, and snippets.

@Molkobain
Last active September 7, 2024 08:25
Show Gist options
  • Save Molkobain/91b8e67cb977e416fb17f68653ee8384 to your computer and use it in GitHub Desktop.
Save Molkobain/91b8e67cb977e416fb17f68653ee8384 to your computer and use it in GitHub Desktop.
Apache 2.4 - Switch PHP versions through URL prefix
##########################################################################################################################
#
# Define which version of PHP to run based on the URL, this allows to use different PHP versions on a same project
# It also allows to use different projects with different PHP versions at the same time
#
# Example 1: https://localhost/php74/my-project will run on PHP 7.4
# https://localhost/php82/my-project will run on PHP 8.2 even though the sources are in the same folder
#
# Prerequisites:
# - Apache 2.4+
# - Several PHP versions already installed
#
# Usage:
# - Copy this file to your Apache conf folder
# - On Unix it might be `/etc/apache2/conf-available`
# - On Windows, for WampServer it should be `C:/wamp64/bin/apache/apache2.4.<X>.<Y>/conf/extra`
# - Include this file the virtual hosts for which you want to enable this
# - On Unix it might be `Include conf-available/php-versions-through-url.conf`
# - On Windows, for WampServer it should be `Include conf/extra/php-versions-through-url.conf`
# - Go the Unix / Windows section below, and follow the steps corresponding to your platform
# - Restart Apache
# - Access your project by prefixing the URL with the desired PHP version
# (eg. for "https://localhost/my-project", type "https://localhost/php74/my-project" to run it on PHP 7.4)
#
##########################################################################################################################
# Uncomment to help debug the URL rewriting rule (output should be in error log)
#LogLevel rewrite:trace6
RewriteEngine On
# First, retrieve PHP version from the URL
SetEnvIf Request_URI "^/php([0-9])([0-9])/" URL_PHP_VERSION=$1.$2
# Then rewrite the URL to ignore it and use the regular URLs defined in virtual hosts
RewriteRule "^/php([0-9])([0-9])/(.*)$" /$3 [L]
#####################
# Unix #
#####################
# - Uncomment and adjust this line to the path of your PHPs
# - URL_PHP_VERSION will be like "8.2", if you need to change the format of this variable, check the `SetEnvIf` line above
#
#<FilesMatch \.php$>
# SetHandler "proxy:unix:/var/run/php/php%{ENV:URL_PHP_VERSION}-fpm.sock|fcgi://localhost/"
#</FilesMatch>
#####################
# Windows #
#####################
## Step 1
# - Enable "proxy_module", "proxy_fcgi_module" and "setenvif_module" modules in Apache configuration
## Step 2
# - PHPROOT should already be defined in your Apache conf, if not uncomment the following line and put the abs. path to you PHP binaries up to the version number
#
#Define PHPROOT ${INSTALL_DIR}/bin/php/php
#
## Step 3
# - Uncomment and adjust these lines to the path and versions of your PHPs
# - URL_PHP_VERSION will be like "8.2", if you need to change the format of this variable, check the `SetEnvIf` line above
#
#<IfModule fcgid_module>
# # Define which binary to use regarding the selected version
# # - Default PHP version used if URL not starting with "/phpXY/"
# FcgidWrapper "${PHPROOT}7.4.33/php-cgi.exe" .php
# # - Available PHP versions
# <If "%{ENV:URL_PHP_VERSION} == '5.6'">
# FcgidWrapper "${PHPROOT}5.6.40/php-cgi.exe" .php
# </If>
# <If "%{ENV:URL_PHP_VERSION} == '7.0'">
# FcgidWrapper "${PHPROOT}7.0.33/php-cgi.exe" .php
# </If>
# <If "%{ENV:URL_PHP_VERSION} == '7.1'">
# FcgidWrapper "${PHPROOT}7.1.33/php-cgi.exe" .php
# </If>
# <If "%{ENV:URL_PHP_VERSION} == '7.2'">
# FcgidWrapper "${PHPROOT}7.2.34/php-cgi.exe" .php
# </If>
# <If "%{ENV:URL_PHP_VERSION} == '7.3'">
# FcgidWrapper "${PHPROOT}7.3.33/php-cgi.exe" .php
# </If>
# <If "%{ENV:URL_PHP_VERSION} == '7.4'">
# FcgidWrapper "${PHPROOT}7.4.33/php-cgi.exe" .php
# </If>
# <If "%{ENV:URL_PHP_VERSION} == '8.0'">
# FcgidWrapper "${PHPROOT}8.0.26/php-cgi.exe" .php
# </If>
# <If "%{ENV:URL_PHP_VERSION} == '8.1'">
# FcgidWrapper "${PHPROOT}8.1.24/php-cgi.exe" .php
# </If>
# <If "%{ENV:URL_PHP_VERSION} == '8.2'">
# FcgidWrapper "${PHPROOT}8.2.11/php-cgi.exe" .php
# </If>
#
# # Define which php.ini file to use
# #
# # Note: Strangely when using the ${URL_PHP_VERSION} variable in the path, Apache doesn't start, but as this directive is mandatory
# # we pass a non existing file path so it will load its default php.ini.
# #
# FcgidInitialEnv PHPRC "${PHPROOT}FAKEPATH/php.ini"
#
# <Files ~ "\.php$">
# Options +Indexes +Includes +FollowSymLinks +MultiViews +ExecCGI
# AddHandler fcgid-script .php
# </Files>
#</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment