Skip to content

Instantly share code, notes, and snippets.

@klickreflex
Last active May 17, 2021 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klickreflex/779683dcc62a7776dcab to your computer and use it in GitHub Desktop.
Save klickreflex/779683dcc62a7776dcab to your computer and use it in GitHub Desktop.
Setting up and Using Environments with htaccess
# This `.htaccess` snippet does the following:
# 1. Create two environments called staging and development based on the host name
# 2. Set up ht authentication only on the stage server (`Deny from env=staging`)
# 3. Rewrite all requests to the main domain (www.mysite.com) if we’re not on either the development nor staging environment and the host doesn't already equal the main domain
AuthName "Staging Server"
AuthType Basic
AuthUserFile /var/sites/mysite/.htpasswd
AuthGroupFile /dev/null
require valid-user
SetEnvIf Host mysite.klickreflex.com env=staging
SetEnvIf Host mysite.dev env=development
Order Allow,Deny
Allow from all
Deny from env=staging
Satisfy Any
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{ENV:env} !development
RewriteCond %{ENV:env} !staging
RewriteCond %{HTTP_HOST} !^mysite\.dev$ [NC]
RewriteCond %{HTTP_HOST} !^www.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com [R=301,L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment