I want to configure a local apache server to serve php files with different php versions. In my document root I have phpinfo.php, now if I go to http://localhost/phpinfo.php4, I want to see the phpinfo.php file processed with php4, if I go to http://localhost/phpinfo.php5 I want to see the same file processed with php5.

Note: both php 4 and 5 are already installed side by side, I have no problem configuring apache to treat files that have a .php4 or .php5 extension on the filesystem with the correct php version. What I want is for apache to do the following:

  • If the url-path ends in .php5, serve the file which has a .php extension on the filesystem using the application/x-httpd-php5 handler.
  • If the url-path ends in .php4, serve the same file with the .php extension on the filesystem using the application/x-httpd-php4 handler.

After a bit of trial & error I figured it out. The magic incantations are

RewriteEngine on
RewriteRule ^(.*)\.php5$ $1.php [H=application/x-httpd-php,PT]
RewriteRule ^(.*)\.php4$ $1.php  [H=php4,PT]

where application/x-httpd-php is the default handler that mod_php5 registers, and php4 is a custom cgi handler that calls php4. The part I had most trouble figuring out is the PT (passthrough) flag, which is necessary for cgi handlers to work. PT causes other modules that do url rewriting to also do their job, and mod_cgi is such a module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.