Apache服务器设置防盗链的方法
首先,找到您的apache设置文件,一般情况下在 /usr/local/apache/conf/httpd.conf或者apache 2.2 的 /usr/local/apache2/conf/extra/httpd-vhost.conf,您可以酌情找到自己的conf文件,windows和freebsd下也一样,然后找到类似如下内容:
这个是discuz X2.5自带rewrite的规则
<VirtualHost *:80>
DocumentRoot /home/www
ServerName www.zhanhelp.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/topic-(.+).html$ $1/portal.php?mod=topic&topicid=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/article-([0-9]+)-([0-9]+).html$ $1/portal.php?mod=view&aid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/forum-(w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/blog-([0-9]+)-([0-9]+).html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/(fid|tid)-([0-9]+).html$ $1/index.php?action=$2&value=$3&%1
</IfModule>
</VirtualHost>
这个是不带rewrite的
<VirtualHost *:80>
DocumentRoot /home/www
ServerName www.zhanhelp.com
</VirtualHost>
在其中加入一段,具体内容如下:
SetEnvIfNoCase Referer "^http://www.zhanehlp.com" local_ref=1
SetEnvIfNoCase Referer "^http://zhanehlp.com" local_ref=1
<FilesMatch ".(txt|doc|mp3|zip|rar|jpg|gif)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
其中站帮网的网站要更换成您的网址,如果有多个,就加多行;
txt|doc|mp3|zip|rar|jpg|gif的是您需要防盗链的文件后缀,中间用|隔开。
另外一种写法,是用正则,这种写法在各个版本的apache比较通用。具体写法如下:
SetEnvIfNoCase Referer "^http://.*.zhanhelp.com" local_ref=1
SetEnvIfNoCase Referer ".*.zhanhelp.com" local_ref=1
<FilesMatch ".(txt|doc|mp3|zip|rar|jpg|gif)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
其中网址的部分有一点区别,用正则写法, 符号代表转义,因为.本身在正则中有自己的作用。
最终改完防盗链+伪静态规则后就会变成如下:
<VirtualHost *:80>
DocumentRoot /home/www
ServerName www.zhanhelp.com
SetEnvIfNoCase Referer "^http://bbs.zb7.com" local_ref=1
SetEnvIfNoCase Referer "^http://zhanhelp.com" local_ref=1
<FilesMatch ".(txt|doc|mp3|zip|rar|jpg|gif)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/topic-(.+).html$ $1/portal.php?mod=topic&topicid=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/article-([0-9]+)-([0-9]+).html$ $1/portal.php?mod=view&aid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/forum-(w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/blog-([0-9]+)-([0-9]+).html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/(fid|tid)-([0-9]+).html$ $1/index.php?action=$2&value=$3&%1
</IfModule>
</VirtualHost>
现在你的网站就可以彻底的仿制盗链了。