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>