Tag Archives: mod_rewrite

Babylonic Apache SetEnv

The Mission

The other day I had a PHP-project based on the ZendFramework where I had to display different logos whether the project was viewd on the preview or on the live system.

Until now I always exchanged the images after puling the live data from the repository. Of course this was one step that sometimes was forgotten, sometimes the correct images where already in the repository so ther was no need to do it – -all in all it was rather anoying. Yes, you could say: “Automate processes that are always the same” but that is not the point today.

I had something different in mind.

For the ZendFramework I already set an environment variable “APPLICATION_ENV” that defined whether the server hosts a development or a production environment.

Why not use that information to display a logo according to the environment?

So I had either the option to call the logo via a PHP-Script that checks the environment variable and returns the correct image.

Or I could use the – already for ZendFramework available – mod_rewrite to do some rewriting voodoo.

The first option would have meant to write a complete PHP-Script and call that every time the logo is asked for. Somewhat much to do for a simple rewriting I thought.

The Quest

So I went for the second option. And it took me some time to get it up and running.

What happened: I took the documentation for mod_rewrite and went through it. I already had the following section in my VirtualHost-Config

SetEnv APPLICATION_ENV development

So for the directory with the logos in I created the following .htaccess-file (Yes, I could also put it into the servers config file…)

RewriteEngine on
RewriteCond %{ENV:APPLICATION_ENV} development [NC]
RewriteRule ^logo(?!-preview)(.*)\.png$   logo-preview$1.png [NC]

Fine I thought and tried that!

No Luck.

Didn’t work

Not after hours of debugging, reading blogposts and forum-entries and rewriting the code one or the other way.

No Luck at all

During these hours of searching the net I found that I wasn’t the only one having that problem, but mostly the solution was to set the environment variable in the Rewrite Engine as well as via SetEnv. But there had to be a different way.

And there was.

The Happy End

After hours I finaly found http://httpd.apache.org/docs/2.0/env.html and there was a paragraph headed “Some Caveats”.
Bingo! Thats It.

SetEnv is called after the mod_rewrite-calls. SetEnvIf before!

So thats the solution. Use SetEnvIf instead of SetEnv.

So I changed my VirtualHost-Config the following way

-  SetEnv APPPLICATION_ENV development
+  SetEnfIf HTTP "HTTP.*" APPLICATION_ENV=development

Yes It takes some performance, because APPLICATION_ENV is now set on every Request, but as long as static Environment variables are set after dynamic ones, that is an SEP to me.

The ZendFramework-Projects behavior does not change as the change is completely transparent to it. But reloading the log now suddenly showed the correct result!

The Polish

Well I thought, fine. But why not set the APPLICATION_ENV according to the called host-name? And therefore moving the SetEnfIf-call from the virtualServer to the projects htaccess-file? At least that’s where I think it belongs to, as it’s a project-specific setting.

So I removed the SetEnvIf-Line from the VirtualHost-Config and placed the following into the projects .htaccess-file.

<IfModule setenvif_module>
    SetEnvIf Host "\.?(preview|stage|staging|development|dev|local)\.?" APPLICATION_ENV=development

As soon as one of preview, stage, staging, dev, development or local is a part of the hostname the APPLICATION_ENV-variable will be set to “development”.

One could now change that to the local environment by adding other names as well, but for me it is sufficient.