Blog

Local .htaccess environment conditionals, HTTPS example

denise 0 Comments Platform Development, Web Development

Adapted from a post by the Nerdary  that basically deals with the problem of having .htaccess files that are different on your local and other environments.

This example deals with the headache of https and not wanting your local environment to automatically redirect to HTTPS. I am only posting the relevant code that sets up the environment variable.

<IfModule mod_rewrite.c>
  RewriteEngine on

  ### SET UP ENVIORNMENTS ###

  # Default environment is master ###
  RewriteRule .* - [E=ENVIRONMENT:master]

  # Environment is set to develop if the server names ends in local.com
  RewriteCond %{SERVER_NAME} .local.com$
  RewriteRule .* - [E=ENVIRONMENT:develop]
  ## END SET UP ENVIRONMENTS ###

  # Redirect HTTP to HTTPS only if environment is master
  RewriteCond %{ENV:ENVIRONMENT} master
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

Another cool thing about this is that we've also created a new $_SERVER['ENVIRONMENT'] variable that we can access in php. 

 

Leave a reply