Zend SetEnv in .htaccess not working

2019-03-18 15:09发布

问题:

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.

回答1:

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.)



回答2:

You can also use:

RewriteRule .* - [E=ENV_NAME_HERE:ENV_VALUE_HERE]


回答3:

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.



回答4:

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



回答5:

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.