Search code examples
.htaccessuser-agent

.htaccess Allow All from Specific User Agent


I have a website I am developing that is also going to be pulled into a web app. I have the following code in my .htaccess file to prevent access from ANYONE that is not on my allowed IP:

Order deny,allow
Deny from all
AuthName "Restricted Area - Authorization Required" 
AuthUserFile /home/content/html/.htpasswd 
AuthType Basic
Require valid-user
Allow from 12.34.567.89 
Satisfy Any

QUESTION: I would like to add an Allow from rule that will ALSO allow a specific HTTP user agent access to the site.

I found this code to redirect if not the user agent:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !=myuseragent
RewriteRule ^files/.*$ / [R=302,L]

But I can't seem to figure out how to turn this into an Allow from rule. Help?

UPDATE

I found the code below to block specific user agents... I would instead like to say "if NOT myuseragent, then block."

<IfModule mod_rewrite.c>
SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT
Deny from env=HTTP_SAFE_BADBOT
</ifModule>

Solution

  •     SetEnvIfNoCase User-Agent .*google.* search_robot
        SetEnvIfNoCase User-Agent .*yahoo.* search_robot
        SetEnvIfNoCase User-Agent .*bot.* search_robot
        SetEnvIfNoCase User-Agent .*ask.* search_robot
         
        Order Deny,Allow
        Deny from All
        Allow from env=search_robot
    

    Htaccess SetEnvIf and SetEnvIfNoCase Examples