in Web & Tech

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 ( 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 env=staging
SetEnvIf Host 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 ^(.*)$ [R=301,L]

On github:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)