Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can htaccess read a server environment variable set in Apache?

When you set a server environment variable in your Apache settings, it's possible to read that variable from PHP using built in functions.

But can you read the variable from an htaccess file?

What I'm trying to accomplish in my htaccess is something along the lines of:

<If var=="stuff">
do things here
</if>

<if var=="different stuff">
do different things here
</if>
like image 825
Tim Jahn Avatar asked Sep 04 '12 19:09

Tim Jahn


1 Answers

Yes it is.

You will probably want to use mod_setenvif functionality, so that module will need to be turned on.

Then you simply do something like:

SetEnvIf Request_URI "\.gif$" image_type=gif

Which would set the environmental variable image_type with a value of gif if the requested file name ends with .gif.

You could then access this (like in RewriteCond) like this:

RewriteCond %{ENV:image_type} gif
RewriteRule ^(.*) $.gif

You can read the full documentation here: http://httpd.apache.org/docs/2.2/env.html

like image 99
Mike Brant Avatar answered Sep 22 '22 14:09

Mike Brant