Search code examples
.htaccessbasic-authentication.htpasswd

How to set up basic authentication for some subdomains but not others using envif


Thanks to some wonderful people here at stack overflow I got my authentication working to demand a password at dev.website.com using this script:

SetEnvIfNoCase Host ^.*dev\.website\.com$ require_auth=true
AuthUserFile /var/www/vhosts/website.com/.htpasswd
AuthName "Password Protected"
AuthType Basic
Order Deny,Allow
Deny from all
Satisfy any
Require valid-user
Allow from env=!require_auth

I also want it to demand a password on: myname.website.com mynamedev.website.com & devmyname.website.com. Basically on anything but "www.website.com" and "website.com".

What is the simplest way to do that?


Solution

  • I would have thought you could do the following

    SetEnv require_auth=true
    SetEnvIfNoCase Host ^www\.website\.com$ require_auth=false
    SetEnvIfNoCase Host ^website\.com$ require_auth=false
    AuthUserFile /var/www/vhosts/website.com/.htpasswd
    AuthName "Password Protected"
    AuthType Basic
    Order Deny,Allow
    Deny from all
    Satisfy any
    Require valid-user
    Allow from env=!require_auth
    

    The first line setting the default case to be authentication is required, the second two lines being the specific cases where authentication is not required.