Zend SetEnv in .htaccess not working

2019-03-18 15:20发布

I installed Zend on my ubuntu homeserver. In my .htaccess file i have the following code:

SetEnv APPLICATION_ENV development

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]

When i echo APPLICATION_ENV in my index.php in the public folder, APPLICATION_ENV is not set.

What am i doing wrong?

Mod rewrite is enabled in apache.

5条回答
Evening l夕情丶
2楼-- · 2019-03-18 15:41

I prefer to put SetEnv in the httpd.conf file for the local server, so that all my local ZF apps run in development mode, and when I push to the staging server it has a flag to Set the environment to staging, then the production server has no such setting so it defaults to production...

This way I can keep the same .htaccess file in source control and when I push it out to the different servers it behaves as expected.

middaparka is right about suExec... suExec prevents you from setting custom environment variables in your .htaccess file - according to remi at the WebFaction forum.

查看更多
贪生不怕死
3楼-- · 2019-03-18 15:43

You can also use:

RewriteRule .* - [E=ENV_NAME_HERE:ENV_VALUE_HERE]
查看更多
劫难
4楼-- · 2019-03-18 15:48

If you are running your home server on a Mac or have suEXEC enabled, there are security measures in place to strip any non-http environment variables that are defined, therefore they won't appear in $_ENV['APPLICATION_ENV']. You CAN, however access these through a PHP function to get the same thing.

$var = apache_getenv('APPLICATION_ENV');

You could even define the environment variable within PHP if you want, although not recommended.

$_ENV['APPLICATION_ENV'] = apache_getenv('APPLICATION_ENV');

I have an almost identical config to what you have, and we just define a global:

define('APPLICATION_ENV', apache_getenv('APPLICATION_ENV'));

Once you do that, it should work as desired.

查看更多
时光不老,我们不散
5楼-- · 2019-03-18 15:57

First You need mod_env installed (and mod_rewrite)

Double check if Apache has the module(s) loaded with either:

apachectl -l or httpd -l

the command might be different according to OS distribution i believe.

For the .htaccess file to work you need AllowOveride All which can be set in different configuration files (and "parts" depending on the way your OS as Apache configured. Your best option is to find out if it is a htaccess file issue or an environment one. Set anywhere in main configuration file:

SetEnv TESTME foobar

restart Apache and echo TEST from a PHP script. If you got it, debug the .htaccess

Note 1: Restrict in your virtual host the usage of .htacess to the folder where you really need it or move the configurations to a virtual host.

Note 2: You probably don't need all those rewrite rules if you have Apache mod_dir installed. Have a look at it

查看更多
\"骚年 ilove
6楼-- · 2019-03-18 16:00

In order to use SetEnv within a .htaccess file, I believe you need to set...

AllowOverride FileInfo

...within the relevant virtualhost directory block. (And then restart the httpd service as per usual.)

Additionally, depending on how you're running PHP, it's possible that such information is being stripped out. (e.g.: suexec will effectively remove all non-HTTP* environment vars.)

查看更多
登录 后发表回答