views:

428

answers:

2

I've got several sites: example.com, example1.com, and example2.com. All of them point to my server's /public_html folder, which is my Apache root folder.

What do I need to add to my .htaccess file to use http authentication only if the user is coming from example2.com? example.com and example1.com should NOT use authentication.

I know I need something like

AuthType Basic
AuthName "Password Required"
AuthUserFile "/path/to/.htpasswd"
Require valid-user

But I only want to require a password if the user is visiting example2.com.

Edit

Using an approach suggested in an answer, I have the following in my .htaccess file:

SetEnvIfNoCase Host ^(.*)$ testauth
<IfDefine testauth>
RewriteRule ^(.*)$ index2.php?q=$1 [L,QSA]
</IfDefine>

I know that the mod_setenvif.c module is enabled (I verified with an <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 <IfModule> block). Any ideas why?

+1  A: 

You shouldn't be putting per-vhost configuration into .htaccess. Instead, put the config block in the VirtualHost block in the proper config file in /etc/apache/sites-enabled/*.

Martin v. Löwis
I don't have access to the vhost files, unfortunately. I'm running all this on a shared hosting server through CPanel. =(
Dave DeLong
+1  A: 

Try this using SetEnvIfNoCase and <IfDefine>:

SetEnvIfNoCase Host ^example2\.com$ auth-req

<IfDefine auth-req>
# …
</IfDefine>
Gumbo
This looks really promising! I've edited my original question with some more regarding this approach.
Dave DeLong
As I understood it, the IfDefine has nothing to do with SetEnvIfNoCase. It would have been nice if it had, though, and this was a discussion in the Apache mailing list here (http://archive.apache.org/gnats/3000). Instead, IfDefine is set with the "-D" parameter on loading of Apache, which is set on Ubuntu at least in /etc/apache2/envvars as: export APACHE_ARGUMENTS="-D dev"
Volomike