服务器Apache服务器设置防盗链的方法


很多网站因为图片过多,图片被盗链后造成服务器无谓的负担,如果您是IIS服务器,请看《IIS服务器防盗链具体设置方法》现在给大家讲解一下如何利用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的规则

01.

02.<VirtualHost *:80>

03. DocumentRoot /home/www

04. ServerName www.zhanhelp.com

05. <IfModule mod_rewrite.c>

06. RewriteEngine On

07. RewriteCond %{QUERY_STRING} ^(.*)$

08. RewriteRule ^(.*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topicid=$2&%1

09. RewriteCond %{QUERY_STRING} ^(.*)$

10. RewriteRule ^(.*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3&%1

11. RewriteCond %{QUERY_STRING} ^(.*)$

12. RewriteRule ^(.*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3&%1

13. RewriteCond %{QUERY_STRING} ^(.*)$

14. RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page\%3D$4&page=$3&%1

15. RewriteCond %{QUERY_STRING} ^(.*)$

16. RewriteRule ^(.*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3&%1

17. RewriteCond %{QUERY_STRING} ^(.*)$

18. RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3&%1

19. RewriteCond %{QUERY_STRING} ^(.*)$

20. RewriteRule ^(.*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3&%1

21. RewriteCond %{QUERY_STRING} ^(.*)$

22. RewriteRule ^(.*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3&%1

23.</IfModule>

24.</VirtualHost>
复制代码这个是不带rewrite的

01.

02.<VirtualHost *:80>

03. DocumentRoot /home/www

04. ServerName www.zhanhelp.com

05.</VirtualHost>
复制代码在其中加入一段,具体内容如下:

01.SetEnvIfNoCase Referer "^http://www.zhanehlp.com" local_ref=1

02.SetEnvIfNoCase Referer "^http://zhanehlp.com" local_ref=1

03.<FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">

04.Order Allow,Deny

05.Allow from env=local_ref

06.</FilesMatch>
复制代码其中站帮网的网站要更换成您的网址,如果有多个,就加多行;
txt|doc|mp3|zip|rar|jpg|gif的是您需要防盗链的文件后缀,中间用|隔开。

另外一种写法,是用正则,这种写法在各个版本的apache比较通用。具体写法如下:

01.SetEnvIfNoCase Referer "^http://.*\.zhanhelp\.com" local_ref=1

02.SetEnvIfNoCase Referer ".*\.zhanhelp\.com" local_ref=1

03.<FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">

04.Order Allow,Deny

05.Allow from env=local_ref

06.</FilesMatch>
复制代码其中网址的部分有一点区别,用正则写法, \ 符号代表转义,因为.本身在正则中有自己的作用。

最终改完防盗链+伪静态规则后就会变成如下:

01.<VirtualHost *:80>

02. DocumentRoot /home/www

03. ServerName www.zhanhelp.com

04. SetEnvIfNoCase Referer "^http://bbs.zb7.com" local_ref=1

05. SetEnvIfNoCase Referer "^http://zhanhelp.com" local_ref=1

06. <FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">

07. Order Allow,Deny

08. Allow from env=local_ref

09. </FilesMatch>

10. <IfModule mod_rewrite.c>

11. RewriteEngine On

12. RewriteCond %{QUERY_STRING} ^(.*)$

13. RewriteRule ^(.*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topicid=$2&%1

14. RewriteCond %{QUERY_STRING} ^(.*)$

15. RewriteRule ^(.*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3&%1

16. RewriteCond %{QUERY_STRING} ^(.*)$

17. RewriteRule ^(.*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3&%1

18. RewriteCond %{QUERY_STRING} ^(.*)$

19. RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page\%3D$4&page=$3&%1

20. RewriteCond %{QUERY_STRING} ^(.*)$

21. RewriteRule ^(.*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3&%1

22. RewriteCond %{QUERY_STRING} ^(.*)$

23. RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3&%1

24. RewriteCond %{QUERY_STRING} ^(.*)$

25. RewriteRule ^(.*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3&%1

26. RewriteCond %{QUERY_STRING} ^(.*)$

27. RewriteRule ^(.*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3&%1

28.</IfModule>

29.</VirtualHost>
复制代码现在你的网站就可以彻底的仿制盗链了,其实discuz的后台附件设置也是可以设置防盗链的,只要设置附件链接有效期.

 

关闭
18988993509

020-82315523