5

I am using a wildcard dns system that routes all subdomains through a single web app and sets a userid based on the first part of the URL (X.domain.com where X is the username).

I now want to edit my htaccess file to enable conditional httpauth using htpasswd for specific domains. e.g. if url = password.domain.com the enable httpauth.

I'm sure this is possible but have limited knowledge of htaccess.

Dan Tudor
  • 61
  • 1
  • 4

1 Answers1

7

You can use SetEnvIf, here's a snippet from this post by Tom Schlick.

#allows a single uri through the .htaccess password protection
SetEnvIf Request_URI "/testing_uri$" test_uri

#allows everything if its on a certain host
SetEnvIf HOST "^testing.yoursite.com" testing_url
SetEnvIf HOST "^yoursite.com" live_url
Order Deny,Allow

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /
Require valid-user

#Allow valid-user
Deny from all
Allow from env=test_uri
Allow from env=testing_url
Allow from env=live_url
Satisfy any
Jay Wick
  • 12,325
  • 10
  • 54
  • 78
Gnurou
  • 7,923
  • 3
  • 22
  • 32
  • Note the URL has changed to: http://tomschlick.com/blog/2009/11/conditional-htpasswd-multi-environments.html – sbuck Dec 02 '11 at 02:51
  • 1
    \*Updated answer\* this is why we avoid just posting links. ([How To Answer](http://meta.stackoverflow.com/help/how-to-answer)) – Jay Wick Apr 16 '14 at 04:30
  • Now its actually https://tomschlick.com/2009/11/08/conditional-htpasswd-multi-environments (wihtout a `/` at the end) – Thomas Kekeisen Oct 12 '16 at 20:30