.htaccess if statements for different .htpasswd files

296 views Asked by At

I'm not entirely sure this is possible, but I have an application that is hosted on a few different URLs and a few different subdomains.

Each of these has a basic protected section (using htpasswd).

I'd love to be able to have ONE htaccess file, rather than having a bunch of different ones that point to different files.

I have the following file structure:

.htaccess .htpasswd-domainone .htpasswd-domaintwo .htpasswd-domainthree

Is there a way to do something like this?

<If "%{HTTP_HOST} == 'domainone'">
AuthUserFile /file/location/.htpasswd-domainone
</If>

<If "%{HTTP_HOST} == 'domaintwo'">
AuthUserFile /file/location/.htpasswd-domaintwo
</If>


<If "%{HTTP_HOST} == 'domainthree'">
AuthUserFile /file/location/.htpasswd-domainthree
</If>

Taking subdomains into account?

1

There are 1 answers

0
Richard On BEST ANSWER

Assuming Apache 2.4, the following works:

Options -Indexes

AuthName Login
AuthType Basic
require valid-user

<If "%{HTTP_HOST} == 'sub.domain1.com'">
AuthUserFile /var/www/html/admin/.htpasswd-domain1
Deny from all
Satisfy any
</If>
<ElseIf "%{HTTP_HOST} == 'sub2.domain1.com'">
AuthUserFile /var/www/html/admin/.htpasswd-domain2
Deny from all
Satisfy any
</ElseIf>
<Else>
Allow from all
Satisfy any
</Else>