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>
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
I just want to allow ONE SPECIFIC user agent rather than trying to block all
Here's my config to allow only wget:
SetEnvIf User-Agent .*Wget* wget
Order deny,allow
Deny from all
Allow from env=wget
Allow from
and Rewrite*
are directives from two different Apache's modules.
The first one is mod_authz_host
and the other from mod_rewrite
.
You can use mod_rewrite
to do what you want:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !=myuseragent
RewriteRule .* - [F,L]
If you don't want to use mode_rewrite, with Apache 2.4 you can use something similar to this:
<Location />
AuthType Basic
AuthName "Enter Login and Password to Enter"
AuthUserFile /home/content/html/.htpasswd
<If "%{HTTP_USER_AGENT} == 'myuseragent'">
Require all granted
</If>
<Else>
Require valid-user
Require ip 12.34.567.89
</Else>
</Location>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With