Jump to content
#FlattenTheCurve ×

Howto Block all USER_AGENT except a custom one

Recommended Posts


I'm trying to have a web page accessible only from inside an android app. I can make the app to provide a custom user agent. For example 

Mozilla/5.0 (MyAPP) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/ Mobile Safari/537.36

I need to edit the .htaccess file to deny all visitors, except the one with the "MyAPP" string in its user agent.

I've tried using this:

RewriteCond %{HTTP_USER_AGENT} ^.*MyAPP*$
RewriteRule .* - [L]
RewriteRule .* http://mydomain.com/error.html [R,L]

but i get all traffic sent to error.html, including the one from my app.

What am I missing here?


Share this post

Link to post
Share on other sites

With further thought, try commenting the 2nd line out?  It appears (I'm not an HTACCESS expert) to me that your 3rd rule is applying to all cases because the 2nd one if being assigned to the RewriteCond class....

Share this post

Link to post
Share on other sites

I'd try this…



RewriteCond %{HTTP_USER_AGENT} !MyApp
RewriteRule ^.* http://mydomain.com/error.html [R=302] 


I think the most critical thing you were missing was the ! at the beginning of the pattern. That ! says "not"… You were matching the string, you needed to match all the ones that don't match the string. I think L is redundant if there's a redirect. And I always do R=302 (or R=301) so I know exactly what type of redirect is being done.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.