Search code examples
.htaccess.htpasswd

.htaccess Condition only apply authentification to staging domain


I'm using the following code to block users on my staging subdomain.

AuthName "PRIVAT"
AuthType Basic
AuthUserFile /var/www/mydomain.com/.htpasswd
require valid-user

Since I'd like to use the same .htaccess for staging and production I'd like to add an condition if HTTP_HOST = staging.mydomain.com so that only the staging environment is password prodected? Is this possible?


Solution

  • Good News: Yes it is possible :-)

    Make use of mod_setenvif directive.

    SetEnvIfNoCase Host ^staging\.mydomain\.com$ SECURED
    
    AuthName "PRIVAT"
    AuthType Basic
    AuthUserFile /var/www/mydomain.com/.htpasswd
    require valid-user
    
    Satisfy    any
    Order      Allow,Deny
    Allow from  all
    Deny from env=SECURED