How to access rewrite rule environment variables set as SetEnv? - apache

I want to access an environment variable inside the rewrite rules which is previously set using SetEnv
e.g.,
SetEnv intl UK
How do I access this "intl" inside the rules later (not in PHP or any other script)?

Sorry, you can't.
The SetEnv directive runs late during request processing meaning that
directives such as SetEnvIf and RewriteCond will not see the variables
set with it.
Please refer to this previous question.
RewriteCond with SetEnv

Related

SetEnv inherit from Host htaccess

With my htaccess file, I'm setting this environment variable
SetEnv CI_ENV development
But I actually want to inherit the environment variable from the host. Something like
SetEnv CI_ENV %{CI_ENV}
I've tried:
SetEnv CI_ENV %{ENV:CI_ENV}
But that doesn't seem to work.
Reason: I'm using docker with a PHP/Apache container. Trying to pass configuration value into it so I'd like to not have anything hardcoded in the htaccess file if possible.
My current workaround is to have a .htaccess.config file with $CI_ENV in the file, and then using a script to substitute the env values, but would be nice to not have to do that.
You don't need to redeclare env variables if you use mod_setevif.
Use this directive in your root .htaccess:
SetEnvIf Host ^ CI_ENV=development
Then use it anywhere in sub-directories without any need of re-delcaring it.
EDIT: Based on comments below, it seems OP is trying to pass system (shell) env variables to Apache.
For that PassEnv directive can be used like this:
# pass system env CI_ENV to Apache
PassEnv CI_ENV
Now CI_ENV will be available as env variables in various Apache directives.

Is it possible to create a dynamic SetEnv in an .htaccess file?

Currently I am trying to set an environment variable to point to a directory but I need it to be dynamic.
Currently I have the following
SetEnv WP_DIR /home/user101/website/public_html/www/app
Is it possible to make that WP_DIR variable get the /home/user101/website/public_html/www part dynamically?
Something like:
SetEnv WP_DIR ${CURRENT_PATH}/app
Please keep in mind that I am really new to this.
SetEnv isn't equip to handle dynamic data as its value. But mod_rewrite can handle this for you. There isn't an ENV variable, to the best of my knowledge, which contains the current absolute path of the directory. But you can write something fancy to grab it:
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*?)/?\1$
RewriteRule ^.*$ - [E=WP_DIR:%{DOCUMENT_ROOT}%2/app]
However setting ENV variables from .htaccess isn't so reliable since they can get prefixed by REDIRECT_ if the URL gets rewritten. You'll have to keep an eye out for REDIRECT_ prefixed variables from PHP.
If you have to do this kind of stuff in the first place, I think you're approaching your code wrong, PHP is well-equipped to process this kind of logic.
If you're trying to rewrite your requests to index.php and get around .htaccess's issue that it adds REDIRECT_ in front of your variable names when you want to set ENV variables from .htaccess for PHP, I'd strongly suggest not using ENV variables in the first place. One approach is RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]. But I dislike this ugly approach since it uses the querystring. The best method I've found is to simply rewrite your requests to index.php then from PHP you'd read strtok($_SERVER["REQUEST_URI"],'?') will contain the request the user requested without the query string. You'd still reference $_GET[...] for such parameters.
You can use PassEnv directive to pass on env variable from system to Apache:
PassEnv CURRENT_PATH
SetEnv WP_DIR ${CURRENT_PATH}/app

htaccess SetEnv REDIRECT_ prefix

I'm setting an environment variable in an htaccess file and it's being prepended with "REDIRECT_". As far as I've read, this is caused by URL rewriting.. the thing is, I'm not doing any rewriting (that I'm aware of).
My webspace contains two files:
.htaccess
SetEnv Foo "bar"
index.php
<?php
print_r($_ENV);
Now, I'm guessing this may have something to do with the fact that this is on a shared hosting package with 1&1.. is there anything else I should be checking or has anyone experienced this before? Or am I just missing something??
Cheers
The issue is triggered by using PHP as a CGI Wrapper. If PHP is running as mod_php apache module it's not prefixing your variables.
Reason for that is internal redirect handling thus apache recreates the variables with the REDIRECT_ prefix :-/
Solution (updated)
Use PHP-FPM or mod_php
If you wanna use CGI Wrapping for PHP put this into your .htaccess:
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
First of all your syntax for SetEnv is wrong. It should be like this:
SetEnv Foo "bar"
Then to access this field from PHP you need to do:
echo $_SERVER["Foo"];
Which should show up as "bar".

mod_rewrite flags that I don't understand

There a few flags that I have been searching through the internet that I don't seem to understand.
When would you use the [E] flag?
For example.
RewriteRule \.jpg$ - [env=dontlog:1]
What does that do...?
And how does the NS (No subsequent requests) work? If I have many includes in my php file, do I need to put it the NS so that it doesnt stop it from working? whats use has it got?
When that rule is matched (\.jpg%), an environment variable dontlog is set with a value of 1.
Later, the most likely scenario for how it is used is that the Apache configuration defines a CustomLog directive which reads that environment variable and does not write a line to the log file when it is set. Therefore, requests for .jpg image files are not written to the Apache log.
For example:
# Log any request that doesn't have a dontlog variable set...
CustomLog logs/access_log common env=!dontlog
There is a little bit of information about environment variables in the mod_rewrite documentation
Directly from said documentation:
The following example sets an environment variable called 'image' to a value of '1' if the requested URI is an image file. Then, that environment variable is used to exclude those requests from the access log.
RewriteRule \.(png|gif|jpg) - [E=image:1]
CustomLog logs/access_log combined env=!image
To answer your question about why the rewrites aren't working, it is because your first rule rewrites the request from index.php to target.php, therefore the second rule will never fire because the requested file is no longer index.php.
The comment already gave you the answer to the environmental variable flag.

SetEnv APPLICATION_ENV development - .htaccess interacting with Zend Framework?

I have the following on my htaccess.
SetEnv APPLICATION_ENV development
When I pass this file to prodution, I will change it to:
SetEnv APPLICATION_ENV production
This
development
and
production
are set on Zend Framework application.ini correct ?
How does Zend and Apache communicate here? How does Zend knows about that htaccess instruction?
Thanks.
SetEnv, used in Apache's configuration (be it a .htaccess file, or a VirtualHost), defines an environment variable.
From PHP, you can read environment variables either :
using the getenv() function.
Or in the $_SERVER or $_ENV superglobal variables
Taking a look at the given index.php in Zend Frameworks QuickStart, you'll see it uses that environment variable the define the PHP constant called APPLICATION_ENV :
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
And that constant is later used to initialize the application :
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
The flow of communication, as you call it, is the followoing:
If you use
SetEnv APPLICATION_ENV production
in your .htaccess, the environment you set there, will be used. Why?
The following piece of code from your index.php doesn't define the constant, if it has been defined already, which is the case, if you use SetEnv in your .htaccess.
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
If your .htaccess doesn't define the constant, the value provided in the index.php will be used. If I were you, I would still keep it in sync. Because you may make mistakes like forgetting to set AllowOverride for your vhost which would result in a situation where the environment is set by the index.php even though the .htaccess is present.