目标:
禁止其他所有网站转载abc.com网站上的gif,jpg,swf等图片和Flash文件,并且当其他网站转载了abc网站的图片后,会返回一个提示图片禁止转载
环境:
[root@mysql teddy]# cat /etc/redhat-release
CentOS release 6.3 (Final)
[root@mysql teddy]# uname -r
2.6.32-279.el6.x86_64
[root@mysql teddy]# /application/apache/bin/apachectl -v
Server version: Apache/2.2.29 (Unix)
Server built: Nov 28 2014 04:49:53
[root@mysql teddy]# /application/apache/bin/apachectl -l|grep mod_rewrite
mod_rewrite.c
要实现防图片盗链,需要用到rewrite_module这个模块来实现
修改网站的配置文件,加入下面绿色部分的内容:
ServerAdmin [email protected]
DocumentRoot "/home/teddy"
ServerName www.abc.com
ServerAlias abc.com
CustomLog "|/usr/local/sbin/cronolog /root/logs/abc.com_logs-%Y%m%d.log" combined
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE image/jpg
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/css
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://abc.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.abc.com/.*$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.abc.com/fuck.png [R,NC]
如何防止图片盗链