开发者

.htaccess Set PHP Value depending on Hostname

开发者 https://www.devze.com 2023-01-07 10:44 出处:网络
for o开发者_运维问答ur project we need to set several PHP values depending on the environment (development/production), most notably session save path and some tracing and profiling settings.

for o开发者_运维问答ur project we need to set several PHP values depending on the environment (development/production), most notably session save path and some tracing and profiling settings.

We do not want to set them in the PHP script because due to some horrible legacy code which would require a lot of changes and we don't want to have to change the .htaccess every time before commiting it to git (but we require the .htaccess to be in source control).

Is there any way to do something like this in the .htaccess:

if (hostname == "dev.example.com") {
  php_value session.save_path /tmp
  [...]
}


You can define rules in Apache's virtualhost config (one step above .htaccess, you'll have to ask an administrator to edit this):

<VirtualHost *:80>                                                              
    ServerName piskvor.example.com
    ServerAlias *.host2.example.com
    php_value session.save_path /home/piskvor/tmp
</VirtualHost>
<VirtualHost *:80>                                                              
    ServerName someotherhost.example.org
    php_value session.save_path /tmp
</VirtualHost>

This gives you the possibility to configure each host differently (by hostname or a hostname wildcard). Most newer Apache setups support this.


At a previous job we used used the following:

<IfDefine SERVER1>
  php_value session.save_path /tmp1
</IfDefine>

<IfDefine SERVER2>
  php_value session.save_path /tmp2
</IfDefine>

You'd then starup apache with the additional switch:

-D SERVER1 SERVER2

EDIT:

  • http://httpd.apache.org/docs/2.0/mod/core.html#ifdefine

EDIT

Maybe something like the following could be a middle-ground?

if (!file_exists('.htaccess')) {

  copy('.htaccess.default', '.htaccess');

  $handle = fopen('.htaccess', 'a');

  switch ($_SERVER['HTTP_HOST']) {

    case 'dev.site':
        fwrite($handle, 'php_value .....' . "\n");
        fwrite($handle, 'php_value .....' . "\n");
    break;

    case 'live.site':
        fwrite($handle, 'php_value .....' . "\n");
        fwrite($handle, 'php_value .....' . "\n");
    break;  

  }

  fclose($handle);

}
0

精彩评论

暂无评论...
验证码 换一张
取 消