Apache防盜鏈幫助

日期:2008-06-13  作者:喜騰小二  來源:PHPChina


最近,大家都比較關心防盜鏈的問題,擔心安裝了SupeSite/X-Space之後,開通網誌的朋友多了
他們上傳的圖片被盜鏈會造成伺服器無謂的負擔,現在我給大家講解一下如何利用Apache本身來防盜鏈 :)

首先,找到您的apache設定檔案,
一般情況下在 /usr/local/apache/conf/httpd.conf
或者apache 2.2 的 /usr/local/apache2/conf/extra/httpd-vhost.conf
您可以酌情找到自己的conf檔案,windows和freebsd下也一樣,然後找到類似如下內容
QUOTE:
這個是帶rewrite的


    DocumentRoot /home/www
    ServerName
www.yourdomin.com
  
    RewriteEngine On
    RewriteRule ^/supesite/([0-9]+)/spacelist(.*)$ /supesite/index.php?$1/action_spacelist$2
    RewriteRule ^/supesite/([0-9]+)/viewspace_(.+)$ /supesite/index.php?$1/action_viewspace_itemid_$2
    RewriteRule ^/supesite/([0-9]+)/viewbbs_(.+)$ /supesite/index.php?$1/action_viewbbs_tid_$2
    RewriteRule ^/supesite/([0-9]+)/(.*)$ /supesite/index.php?$1/$2
    RewriteRule ^/supesite/([0-9]+)$ /supesite/index.php?$1
    RewriteRule ^/supesite/action_(.+)$ /supesite/index.php?action_$1
    RewriteRule ^/supesite/category_(.+)$ /supesite/index.php?action_category_catid_$1
    RewriteRule ^/supesite/itemlist_(.+)$ /supesite/index.php?action_itemlist_catid_$1
    RewriteRule ^/supesite/viewnews_(.+)$ /supesite/index.php?action_viewnews_itemid_$1
    RewriteRule ^/supesite/viewthread_(.+)$ /supesite/index.php?action_viewthread_tid_$1
    RewriteRule ^/supesite/index([.a-zA-Z0-9]*)$ /supesite/index.php


QUOTE:
這個是不帶rewrite的


    DocumentRoot /home/www
    ServerName www.yourdomin.com

在其中加入一段,俱體內容如下:
QUOTE:
SetEnvIfNoCase Referer "^http://www.yourdomin.com" local_ref=1
SetEnvIfNoCase Referer "^http://yourdomin.com" local_ref=1

Order Allow,Deny
Allow from env=local_ref

其中紅色的是您的網址,如果有多個,就加多行
綠色的是您需要防盜鏈的檔案尾碼,中間用|隔開

還一種寫法,是用規則的,這種寫法在各個版本的apache比較通用。

寫法是
QUOTE:
SetEnvIfNoCase Referer "^http://.*.yourdomin.com" local_ref=1
SetEnvIfNoCase Referer ".*.yourdomin.com" local_ref=1

Order Allow,Deny
Allow from env=local_ref

其中紅色的部分有一點區別,用規則寫法,  符號代表轉義,因為.本身在規則中有自己的作用。


最終改完就變成了
QUOTE:

    DocumentRoot /home/www
    ServerName www.yourdomin.com
    SetEnvIfNoCase Referer "^http://www.yourdomin.com" local_ref=1
    SetEnvIfNoCase Referer "^http://yourdomin.com" local_ref=1
  
       Order Allow,Deny
       Allow from env=local_ref
  

  
    RewriteEngine On
    RewriteRule ^/supesite/([0-9]+)/spacelist(.*)$ /supesite/index.php?$1/action_spacelist$2
    RewriteRule ^/supesite/([0-9]+)/viewspace_(.+)$ /supesite/index.php?$1/action_viewspace_itemid_$2
    RewriteRule ^/supesite/([0-9]+)/viewbbs_(.+)$ /supesite/index.php?$1/action_viewbbs_tid_$2
    RewriteRule ^/supesite/([0-9]+)/(.*)$ /supesite/index.php?$1/$2
    RewriteRule ^/supesite/([0-9]+)$ /supesite/index.php?$1
    RewriteRule ^/supesite/action_(.+)$ /supesite/index.php?action_$1
    RewriteRule ^/supesite/category_(.+)$ /supesite/index.php?action_category_catid_$1
    RewriteRule ^/supesite/itemlist_(.+)$ /supesite/index.php?action_itemlist_catid_$1
    RewriteRule ^/supesite/viewnews_(.+)$ /supesite/index.php?action_viewnews_itemid_$1
    RewriteRule ^/supesite/viewthread_(.+)$ /supesite/index.php?action_viewthread_tid_$1
    RewriteRule ^/supesite/index([.a-zA-Z0-9]*)$ /supesite/index.php


好了,之後您重新啓動apache,至此您的盜鏈命運就結束了,呵呵
祝您辦站生涯愉快!!!

<<<返回技術中心

技術文章

站內新聞

我要啦免费统计