一、Apache禁止指定的user_agent
1、修改apache的虚拟主机配置文件
[root@daixuan ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.daixuan.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.123.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
RewriteCond %{HTTP_USER_AGENT} ^.*curl* [NC,OR] 定义条件,NC不区分大小写
RewriteCond %{HTTP_USER_AGENT} ^.*chrome* [NC] 定义或者chrome访问条件
RewriteRule .* - [F] 规则是禁用,Forbidden
</IfModule>
</VirtualHost>
2、测试结果:chrome不能打开www.test.com;Firefox可以打开www.test.com
[root@daixuan ~]# curl -I www.test.com curl访问也被禁止了
HTTP/1.1 403 Forbidden
二、通过Rewrite限制访问目录
1、正常可以访问
在防盗链中取消.txt文件被盗用,在/data/www/tmp/12.txt中输入123456
浏览器可以正常打开:http://www.test.com/tmp/12.txt 123456
2、修改配置文件
[root@daixuan ~]#vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.daixuan.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.123.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
# RewriteCond %{HTTP_USER_AGENT} ^.*curl* [NC,OR]
# RewriteCond %{HTTP_USER_AGENT} ^.*chrome* [NC]
# RewriteRule .* - [F]
RewriteCond %{REQUEST_URI} ^.*/tmp/.* [NC] 条件是包含‘/tmp/’目录的文件
RewriteRule .* - [F] 拒绝访问
</IfModule>
</VirtualHost>
3、测试
Firefox浏览器打开:http://www.test.com/tmp/12.txt ,特定目录访问限制成功。
You don't have permission to access /tmp/12.txt on this server.