Conditional PHP Version in .htaccess

时光毁灭记忆、已成空白 提交于 2019-12-30 11:17:11

问题


On my local testing server I'm using PHP 5.4. On the live host, however, I'm currently limited to PHP 5.3 and it must be specified in .htaccess (otherwise it defaults to 5.2).

So I can add:

# Use PHP 5.3
Action application/x-hg-php53 /cgi-sys/php53
AddHandler application/x-hg-php53 .php

which works great on the live host but breaks the local testing server.

Is it possible to use something like mod_rewrite to conditionally specify a PHP version based on the domain? Or is there a better way? The .htaccess file is tracked (git) and I'd like to keep it that way if possible.

I think the same question is being asked here, but I don't have a dynamic deployment method implemented (other than some simple bash during git push/pull).


回答1:


There's no way to do this dynamically using mod_rewrite.

You could try pointing your Action to a neutral php wrapper that can detect whether it needs to run cgi-sys/php53 or the PHP 5.4 version. Something along the lines of:

Action php-cgi /cgi-sys/php-wrapper.cgi
AddHandler php-cgi .php

Then in the cgi-sys/php-wrapper.cgi file, something like:

#!/bin/sh

# check if this is the php 5.3 environment
if [ -x "/path/to/cgi-sys/php53" ]; then
    # execute php 5.3
    export PHPRC=/path/to/php5.3/rc
    exec /path/to/cgi-sys/php53
else 
    # else, run php 5.4
    export PHPRC=/path/to/php5.4/rc
    exec /path/to/cgi-sys/php54
fi


来源:https://stackoverflow.com/questions/18857939/conditional-php-version-in-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!