title: Use HTTP Auth only if accessing a specific domain
body: <p>I've got several sites:  <code>example.com</code>, <code>example1.com</code>, and <code>example2.com</code>.  All of them point to my server's <code>/public_html</code> folder, which is my Apache root folder.</p>
  
  <p>What do I need to add to my <code>.htaccess</code> file to use http authentication only if the user is coming from <code>example2.com</code>?  <code>example.com</code> and <code>example1.com</code> should NOT use authentication.</p>
  
  <p>I know I need something like</p>
  
  <pre><code>AuthType Basic
  AuthName "Password Required"
  AuthUserFile "/path/to/.htpasswd"
  Require valid-user
  </code></pre>
  
  <p>But I only want to require a password if the user is visiting <code>example2.com</code>.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Using an approach suggested in an answer, I have the following in my .htaccess file:</p>
  
  <pre><code>SetEnvIfNoCase Host ^(.*)$ testauth
  &lt;IfDefine testauth&gt;
  RewriteRule ^(.*)$ index2.php?q=$1 [L,QSA]
  &lt;/IfDefine&gt;
  </code></pre>
  
  <p>I know that the mod_setenvif.c module is enabled (I verified with an &lt;IfModule> block), but it would appear that "testauth" is never getting defined, because my test to verify (redirecting to index2.php) is not executing (whereas it was getting executed in my &lt;IfModule> block).  Any ideas why?</p>