1

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?

8
  • 1
    SetEnvIf works on request headers. So you should use HOST not HTTP_HOST. Forexample : SetEnvIf HOST ^www\. host_with_www sets the env if host is starting with www. Then you can use it as %{host_with_www} Commented Nov 23, 2015 at 13:53
  • Sorry that was a typo, I've amended it now. Commented Nov 23, 2015 at 13:59
  • Read how SetEnvIf and SetEnvIfNoCase works - httpd.apache.org/docs/2.2/mod/mod_setenvif.html Commented Nov 23, 2015 at 14:03
  • I did, I dont understand, thats why I asked for help, not a link. Commented Nov 23, 2015 at 15:45
  • Your htaccess Rule has a condition above it RewriteCond %{Env:envoirment} production it checks if the envoirnment variable value is production (if it set) then redirect to example.com. I think your rule is working . because SetEnvIf envoirnment ^(.*)$ environment=production has already set the variable. Commented Nov 23, 2015 at 16:04

1 Answer 1

11

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.