PDA

View Full Version : .htaccess help - user_agent conditional protection syntax


skippybosco
07-09-2009, 11:27 AM
Hello all.

I have a protected podcast that I make available via my website as well as through iTunes.

For visitors streaming the podcast on my site I have a session based cookie that is set and use rewrite rules to either allow access (if cookie is exists and is valid) or redirect to a styled login page using aMember.

iTunes, however, only supports Basic Auth protection. If I switch my .htaccess over to that, it pops up a non-styled browser login/password dialog for my web visitors, even if they have an existing cookie established (bummer!)

Ideally, I'd like to configure an .htaccess conditional rule that says:

If user_agent = itunes do this:

AuthType Basic
AuthName "Members Only"
AuthUserFile /home/mydomain.com/amember/data/.htpasswd
AuthGroupFile /home/mydomain.com/amember/data/.htgroup
Require valid-user

else, everyone else do this:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_COOKIE} amember_nr=([a-zA-Z0-9]+)
RewriteCond /home/mydomain.com/amember/data/new_rewrite/%1 -f
RewriteRule ^(.*)$ - [L]

Is this kind of logic possible?

Can anyone provide guidance on the correct syntax?

I'm guessing some use or combination of:

SetEnvIfNoCase "User-Agent" "itunes" itunes

Thanks in advance!!

clunk.werclick
08-02-2009, 09:37 AM
Something like this (untested);

# Rewrite requests if user agent not Itunes
RewriteCond %{HTTP_user_agent} !^iTunes
RewriteRule .* errorpage.html [L]

skippybosco
08-02-2009, 10:44 AM
@Clunk, Thank you for your reply.

How would that get nested to include a:

if iTunes
do this rewrite

if NOT iTunes
do this rewrite

Struggling to figure out how to manage else if statements in .htaccess for rewrite rules.

Thanks again!

clunk.werclick
08-02-2009, 12:06 PM
My guess (and I've not studied it so guess is what it is) would be add another rule under this one. Itunes would have already been evaluated and tested true by this rule and the redirect should have happened. This would, in theory, only leave Non Itunes behind.

Or that is what I assume would happen ? This could be flawed so test it. Myself, I don't use it, so I'm probably not the best one to answer and it's nothing more than a guess - I can't stress that enough.