Zend SetEnv in .htaccess not working
Asked Answered
S

5

12

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.

Sequential answered 23/4, 2011 at 13:39 Comment(1)
Do you have mod_env enabled on your server?Birefringence
M
18

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

Mon answered 23/4, 2011 at 13:58 Comment(2)
AllowOverride All will allow for it to read the whole .htaccess fileRadium
Thanks, that makes me see my current server is not allowing this, unfortunately... =/Kerry
G
6

You can also use:

RewriteRule .* - [E=ENV_NAME_HERE:ENV_VALUE_HERE]
Galenical answered 18/1, 2015 at 0:47 Comment(0)
L
5

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

Lyricism answered 20/5, 2011 at 11:16 Comment(1)
On my Ubuntu machine, the -l switch shows compiled in modules. The -M switch shows all loaded modules (which is what I assume you are after).Lassie
C
4

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.

Canotas answered 23/4, 2011 at 21:2 Comment(0)
A
4

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.

Actinology answered 28/7, 2013 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.