apache 防盗链的第一种实现方法,可以用 rewrite 实现。
首先要确认 apache 的 rewrite module 可用:能够控制 apache httpd.conf 文件的,打开 httpd.conf,确保有这么一行配置:
loadmodule rewrite_module modules/mod_rewrite.so
<VirtualHost *:80>
#加在这个下面 CustomLog "-----------" combined
RewriteEngine On
#RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !^https://.*.hak.red/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://.*.hak.red$ [NC]
RewriteRule .*\.(gif|jpg|swf|css|js|png|ico)$ http://error.hak.red/ [R,NC]
#只允许 *.hak.red 访问 gif|jpg|swf|css|js|png|ico 否则跳转到 http://error.hak.red/
#如果开通了 SSL 加在这个<VirtualHost *:443>里面
#*(我没用这个)*/
RewriteCond %{HTTP_REFERER} !^$ [NC]
允许空的来源,即用户浏览器手动属于则允许访问文件。
也可以加到.htaccess文件里
.htaccess每次访问都需要读取分析,效率很低。