Skip to content

Instantly share code, notes, and snippets.

@mecmartini
Last active May 24, 2018 09:53
Show Gist options
  • Save mecmartini/41ae12fb14cf0be525b259c7d1dff154 to your computer and use it in GitHub Desktop.
Save mecmartini/41ae12fb14cf0be525b259c7d1dff154 to your computer and use it in GitHub Desktop.
Example of .htaccess environment conditionals
<IfModule mod_rewrite.c>
RewriteEngine on
# SET UP DEFAULT ENVIORNMENT TO MASTER
RewriteRule .* - [E=ENVIRONMENT:master]
# SET UP ENVIORNMENT TO DEVELOP IF WE ARE ON localhost OR .dev OR .local
RewriteCond %{SERVER_NAME} localhost [OR]
RewriteCond %{SERVER_NAME} .dev$ [OR]
RewriteCond %{SERVER_NAME} .local$
RewriteRule .* - [E=ENVIRONMENT:develop]
# SET UP ENVIORNMENT TO STAGE IF WE ARE ON stage.
RewriteCond %{SERVER_NAME} ^stage.
RewriteRule .* - [E=ENVIRONMENT:stage]
# DEVELOP
# ...
# STAGE
# ...
# Redirect HTTP to HTTPS only if environment is set to master
RewriteCond %{ENV:ENVIRONMENT} master
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment