Is it possible to use a macro with the value of a environment variable?
E.g.
<Macro setLog $name>
CustomLog "|cronolog -l /var/www/logs/$name/access.log /var/www/logs/$name/%Y-%m-%d_access.log" logging
ErrorLog "|cronolog -l /var/www/logs/$name/error.log /var/www/logs/$name/%Y-%m-%d_error.log"
</Macro>
SetEnvIf Host soup* path=soup
use setLog path
Which would 'print' the macro with path. Can I use the value for path? (in this case soup)
No, the internal variables modified by SetEnv and SetEnvIf are during request processing. Your macros are expanded when the configuration is parsed -- the logs are also opened up front and can't depend on parameters of the request.
Related
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.
Back Story:
I have a copy of all my website files located on two different file servers. I have a two web servers, mimir2 and mimir4, with maps to access the files from either of these file servers. The unique value in the file path to these file servers is either dtg_devel_ah or dtg_devel_nr.
What I am trying to do:
I am editing my httpd.conf file to dynamically set, on server start, which file server to use based on the web servers name. I am doing this by using Apaches Define directive to create the variable dtg_devel_path which will get used in directory paths throughout the rest of the httpd.conf file.
My Code to accomplish this (Note: There are SetEnv declrations scattered throughout only for my debugging purposes):
# Define which dtg_devel_xx to use based on server name. This is so that
# the server is accessing its local files instead of across the country
# Default to dtg_devel which will point to which ever server site is active
Define dtg_devel_path dtg_devel
# If running on mimir2 use dtg_devel_nr
# else if run on mimir4 use dtg_devel_ah
<If "env('HOSTNAME') =~ /mimir2/i">
Define dtg_devel_path dtg_devel_nr
SetEnv dtg_devel_m2 ${dtg_devel_path}
SetEnv dtg_devel_hostname_m2 ${HOSTNAME}
</If>
<ElseIf "env('HOSTNAME') =~ /mimir4/i">
Define dtg_devel_path dtg_devel_ah
SetEnv dtg_devel_m4 ${dtg_devel_path}
SetEnv dtg_devel_hostname_m4 ${HOSTNAME}
</ElseIf>
SetEnv dtg_devel_final_set_path ${dtg_devel_path}
SetEnv dtg_devel_hostname ${HOSTNAME}
When I run this the values getting set are:
My Problem:
The value of dtg_devel_path should be dtg_devel_nr. I am running this on server mimir2 which you can see is confirmed from the above picture. The if part of the if/elseif statement is returning true (HOSTNAME equals mimir2) and in turn setting dtg_devel_path, dtg_devel_hostname_m2, and dtg_devel_m2. Strangely though somehow dtg_devel_path then gets changed to the value dtg_devel_ah even though the elseif block never gets entered. This is confirmed by the value of the dtg_devel_final_set_path variable and the lack of values for dtg_devel_m4 and dtg_devel_hostname_m4. This code block is the only location that the string dtg_devel_ah appears so this has to be where it is getting set.
Does anyone see the mistake that I have in my code? Does anyone have any suggestions? Any help would be greatly appreciated!! If any additional information is needed please let me know.
I could not figure out how to solve this completely through Apache so I ended up going a slightly different path. My web server is running on a RHEL 6.4 machine fyi.
I basically pulled the if/else function that I had in the http.conf file and put it into three places; /etc/profile.d/dtg_deve_path.<sh,csh> and /etc/init.d/httpd. These files check the host name and create a DTG_DEVEL_PATH environment variable based on that value. I then access that value in httpd.conf with a Define dtg_devel_path ${DTG_DEVEL_PATH} which then lets me accomplish what I was trying to do, use ${dtg_devel_path} through the rest of the httpd.conf file to dynamically specify which file server to use.
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
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.
I need to set my apache environment to 'foobar'
I know I need to set in in my vhost, but what should I type there and where?
SetEnv sets a particular variable to some value, so you need something like
SetEnv varname varvalue
If this is for a specific virtual host, and you have access to the Apache configuration files, this would go inside the <VirtualHost> directive for that virtual host.
If you don't have control of the config files, you'll need to put it in a .htaccess file. But for this to work, SetEnv must be allowed in .htaccess files, which is specified using the AllowOverride directive.
I came here because I configured a python/django WSGI environment.
Loading modules and SetEnv'ing all day long.
Did not work. The vars would just not show up.
What did work was this: https://gist.github.com/GrahamDumpleton/b380652b768e81a7f60c
Graham describes how to basically clone your wsgi.py file and then use a different one for each environment (production, test, dev) and setting your environment vars in there with os.environ['whatever'] = 'you like!'