Set an environment variable in .htaccess and retrieve it in PHP
Assuming your configuration has AllowOverrides with .htaccess, you must enable mod_env in Apache for this to work.
Apache - mod_env
Setting environment variable using .htaccess
Check that the htaccess is being read at all, make sure you've configured your host so that it allows (at the very least) "FILEINFO", or better yet, set it to "ALL"
AllowOverride ALL
If not, make sure your mod_env module is being loaded in your server config.
Make sure you're looking in the right place. Apache internal environment variables are passed along to php via the
$_SERVER[]
array (on the phpinfo() page, under Apache Environment) and they are different from php runtime environment variables.(which are under Environment),
how can I define variable in .htaccess file and use it?
You can't use SetEnv
that way because it's not processed yet.
The internal environment variables set by this directive are set after
most early request processing directives are run, such as access
control and URI-to-filename mapping. If the environment variable
you're setting is meant as input into this early phase of processing
such as the RewriteRule directive, you should instead set the
environment variable with SetEnvIf.
What you are looking for is setenvif
instead of SetEnv
So you should be able do something like
SetEnvIf Request_URI "^.*" base_path=/cig/base3
http://httpd.apache.org/docs/2.2/mod/mod_setenvif.html#setenvif
Can htaccess read a server environment variable set in Apache?
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
Reading environnement variables set from htacess inside php code
the local server I'm using is the built in local symfony server.
.htaccess
is an Apache config file. If you are using the built-in symfony web server (or the PHP built-in web server) then this is not Apache and the .htaccess
file is not going to be processed (and the env vars are not going to be set).
If you need the .htaccess
file to be processed for your application logic then you'll need to run Apache, or move this logic to your application/PHP.
Apache htaccess SetEnv: how concatenate environment variable in htaccess?
%{DOCUMENT_ROOT}
variable is part of mod-rewrite
so
You can set env for a document root using SETenv
directive, try this mod-rewrite
based example :
RewriteEngine on
RewriteRule ^ - [E=SpecialPath:%{DOCUMENT_ROOT}/dir/,L]
Related Topics
How to Detect Country/Location of Visitor
Differencebetween Sessions and Cookies in PHP
How to Identify Server Ip Address in PHP
How to Make Pdo Run Set Names Utf8 Each Time I Connect, in Zendframework
PHP Conditionals, Brackets Needed
How to Password Protect Folder/Page Using PHP Without a Db or Username
PHP Built in Server and .Htaccess Mod Rewrites
Laravel 4 - Logging SQL Queries
How to Store an Array in a File to Access as an Array Later with PHP
Setting PHP Tmp Dir - PHP Upload Not Working
Why Does Crypt/Blowfish Generate the Same Hash with Two Different Salts
What Factors Make PHP Unicode-Incompatible
How Long Can a Tld Possibly Be
Get Nearest Places on Google Maps, Using MySQL Spatial Data
Selecting All Columns That Start with Xxx Using a Wildcard