htaccess SetEnvIf true - .htaccess

If I want to set an environment variable before RewriteRules are evaluated, I have to use SetEnvIf instead of SetEnv. However, SetEnvIf requires one to have a condition. As it is, I have:
SetEnvIf Request_Method ^ ENV=VALUE
Is there a better way to do this?

You can use mod_rewrite's E flag:
RewriteRule ^ - [E=ENV:VALUE]
Which will guarantee that it gets set before (or after) rules get applied.

Using SetEnvIf you can do something like:
SetEnvIf ENV ^(.*)$ ENV=VALUE

Related

Dynamic environment variable in .htaccess

I'm struggling to figure out how to get environment variables to work using SetEnvIf based off my scenario, and was wondering if somebody can tell me how to do it, or give an example.
My outcome is that I need the following redirect to fire based off the environment vars, I've got other cases that will also need to use this logic.
I've got the below, so the redirect only happens on production
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:environment} production
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
I was hoping that I could setup the following, however cannot figure it out.
SetEnvIf environment ^(.*)$ environment=production #set it to production, if it is not already set?
SetEnvIf Host ^staging. environment=staging
SetEnvIf Host .dev$ environment=wamp
Ideally my psudo code would be
SetEnvIf environment is not set, set to production
SetEnvElse host starts with staging, set to staging
SetEnvElse host ends with dev, set to wamp
Then in my PHP I've got
<?php
if( getenv('environment') ){
exit(getenv('environment'));
}
else {
exit("environment not found");
}
And my output is definitely
environment not found
I'm accessing owen.jekyll-test.dev
Can anybody point me in the direction as to what I'm doing wrong?
After many hours, I've finally got a working solution that allows a dynamic .htaccess based off environments
For anybody who is interested in a similar setup, here is our configuration which automatically handles SSL, WWW Redirects, Apache Auth and Is_Admin flags.
# ----------------------------------------------------------------------
# | Admin Vars |
# ----------------------------------------------------------------------
SetEnvIf Remote_Addr ^43\.432\.136\.23 is_admin=1 # Virgin Media
SetEnvIf Remote_Addr ^81\.43\.184\.70 is_admin=1 # BT
SetEnvIf Remote_Addr ^164\.23\.234\.6 is_admin=1 # Orbtalk
SetEnv office_ip 132.39.322.23
# ----------------------------------------------------------------------
# | Environment Detection |
# ----------------------------------------------------------------------
SetEnvIf environment .+ env_defined=$0
SetEnvIf environment ^(.*)$ environment=production
SetEnvIf Host ^staging. environment=staging
SetEnvIf Host .dev$ environment=dev
SetEnvIf env_defined .+ environment=$0
SetEnvIf prefix ^(.*)$ prefix=www.
SetEnvIf Host ^www !prefix
# ----------------------------------------------------------------------
# | Password Protection |
# ----------------------------------------------------------------------
AuthType Basic
AuthName "Protected Login"
AuthUserFile /var/sites/website/.htpasswd
Order deny,allow
Deny from all
Satisfy any
SetEnvIf environment "dev" allow
SetEnvIf environment "production" allow
Allow from env=is_admin
Allow from env=allow
Allow from env=noauth
Require valid-user
Require user my_username
# ----------------------------------------------------------------------
# | Forcing `https://` |
# ----------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteCond %{ENV:environment} production
RewriteRule ^(.*)$ https://%{ENV:prefix}%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# ----------------------------------------------------------------------
# | Forcing the `www.` at the beginning of URLs |
# ----------------------------------------------------------------------
#
# NOTE: IF THE WEBSITE USES SSL, YOU'LL NEED TO MODIFY THE REWRITE URL LOCATION AND MODIFY THE CONDITION
#
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{ENV:environment} production
RewriteRule ^ https://%{ENV:prefix}%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

htaccess deny from lasts nine numbers

I have a little question,
how can I use deny from on my .htaccess to deny connection from:
111.111.111.11* ?
I tried to use * and *.* but didn't work.
I would deny all number from
111.111.111.110
to
111.111.111.119
Any ideas?
I think you'll need to use SetEnvIf, like so:
SetEnvIf Remote_Addr ^111\.111\.111\.11[0-9]$ GetOut=1
Order allow,deny
Allow from all
Deny from env=GetOut
See the docs for SetEnvIf and a document about Access Control with a similar example.
Deny from doesn't take regex but you can use mod_rewrite to that effect:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} 111\.111\.111\.11[0-9]
RewriteRule ^ - [F]

SetEnv for a dynamic URL

what I am trying to do is to use SetEnv for a dynamic URL.
In example for index.php I do this
<IfModule somemodule.c>
<Files index.php>
SetEnv somevariable
</Files>
</IfModule>
However If I wanted to do this for index.php?something=123 then what should I put in my .htaccess? Will it work with SetEnvIf request_URI somehow cause I couldn't get it to work. Another thing I think could be the solution is to use rewrite_mod but I have no idea how to do it. Thanks in advance for any help!
You can use mod_rewrite's E flag to set environment variables:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^something=123$
RewriteRule ^index\.php$ - [L,E=somevariable]

set environment variable with specific HTTP header in htaccess

I'm trying to set an Environment variable if I got a specific HTTP Header sent.
So I tried a few different way that I will detail
Header set MY_HTTP_HEADER "1"
# tests with with SetEnvIf
SetEnvIf %{HTTP:MY_HTTP_HEADER} ^1$ THE_ENV=ok
SetEnvIf MY_HTTP_HEADER ^1$ THE_ENV=ok
SetEnvIf %{MY_HTTP_HEADER} ^1$ THE_ENV=ok
# tests with RewriteRule
RewriteCond %{HTTP:MY_HTTP_HEADER} ^1$
RewriteRule .* index.php [L,E=THE_ENV:ok]
RewriteCond MY_HTTP_HEADER ^1$
RewriteRule .* index.php [L,E=THE_ENV:ok]
There is something that I certainly missed, because all the codes above doesn't work.
EDIT
The correct one is SetEnvIf MY_HTTP_HEADER ^1$ THE_ENV=ok and like #anubhava pointed it doesn't work if you set the header in the same .htaccess so I created another page calling the actual page with CURL with this header curl_setopt($curl,CURLOPT_HTTPHEADER,array('MY_HTTP_HEADER: 1'));
You're making 2 mistakes:
1) This directive:
Header set MY_HTTP_HEADER "1"
Actually sends response header not request header. Use it like this to set request header:
RequestHeader set X-MY-HTTP-HEADER "1"
2) You're setting request header and checking for it in the same .htaccess. Try sending header in web request from browser (using some Rest client addon) and then you will find THE_ENV=ok env value in your index.php

SetEnvIf not working on specific URL

I've got a password protected site, and I'm trying to allow a specific URL through so that it works for a Payment callback. The site is built using CakePHP.
The below works great however the Allow from env=allow is just not being taken into account (I've tried with my own IP address too). The setenvif mod is enabled in Apache and the other "Allow from" lines work fine. FYI it's running on Ubuntu on EC2. I've also searched on the site for similar issues and solutions but to no avail.
I've checked the $_SERVER global array in PHP for the "allow" environment variable and it exists so running out of ideas. Any help would be much appreciated!
SetEnvIf Request_URI ^/secure_trading/callback allow=1
SetEnvIf Request_URI ^/secure_trading/callback$ allow=1
SetEnvIf Request_URI "/secure_trading/callback" allow=1
SetEnvIf Request_URI "/app/weboot/secure_trading/callback" allow=1
AuthName "Protected"
AuthGroupFile /dev/null
AuthType Basic
AuthUserFile /var/www/domain.co.uk/.htpasswd
Order deny,allow
Satisfy Any
Deny from all
Allow from 127.0.0.1
Allow from env=allow
require valid-user
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

Resources