Apache服務器設置防盜鏈的方法
首先,找到您(nin)的(de)apache設置(zhi)文件,一般情況(kuang)下(xia)在 /usr/local/apache/conf/httpd.conf或者apache 2.2 的(de) /usr/local/apache2/conf/extra/httpd-vhost.conf,您(nin)可以酌情找到自己的(de)conf文件,windows和(he)freebsd下(xia)也一樣(yang),然后找到類似如下(xia)內容:
這(zhe)個是(shi)discuz X2.5自帶rewrite的(de)規則
<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>
這個是不(bu)帶rewrite的
<VirtualHost *:80>
DocumentRoot /home/www
ServerName www.zhanhelp.com
</VirtualHost>
在(zai)其(qi)中加(jia)入(ru)一段(duan),具體(ti)內(nei)容如下:
SetEnvIfNoCase Referer "^//www.zhanehlp.com" local_ref=1
SetEnvIfNoCase Referer "^//zhanehlp.com" local_ref=1
<FilesMatch ".(txt|doc|mp3|zip|rar|jpg|gif)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
其中站幫網(wang)(wang)的網(wang)(wang)站要(yao)更換成您的網(wang)(wang)址,如(ru)果有多個(ge),就加多行;
txt|doc|mp3|zip|rar|jpg|gif的(de)(de)是您需要防(fang)盜鏈的(de)(de)文(wen)件后綴(zhui),中(zhong)間用|隔開。
另外(wai)一種(zhong)(zhong)寫法,是用正(zheng)則,這種(zhong)(zhong)寫法在各個版本的apache比較通用。具體寫法如下:
SetEnvIfNoCase Referer "^//.*.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>
其中(zhong)網址的(de)部分有一點區別,用正(zheng)則(ze)寫(xie)法, 符(fu)號代表(biao)轉義,因為.本身在(zai)正(zheng)則(ze)中(zhong)有自己的(de)作用。
最終(zhong)改(gai)完防盜鏈+偽(wei)靜態規則(ze)后(hou)就會變(bian)成如下:
<VirtualHost *:80>
DocumentRoot /home/www
ServerName www.zhanhelp.com
SetEnvIfNoCase Referer "^//bbs.zb7.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>
<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>
現在你的網站就可以(yi)徹底的仿(fang)制盜鏈了。
