Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Apache .htaccess 圖片防盜鏈完整配置指南:允許搜尋引擎、自定義替換圖片

編輯:關於伺服器

防盜鏈核心原理

利用 mod_rewrite 檢查 HTTP Referer 標頭:

  • **允許:** 空 Referer (直接輸入網址)、本站域名、主流搜尋引擎 (Google、Baidu、Bing 等)。
  • **拒絕/替換:** 其他域名引用 → 返回 403 或重導向至替換圖片 (如版權宣告圖)。

完整 .htaccess 配置範例

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # 保護 .htaccess 自身
    <Files ~ "^\.ht">
        Order allow,deny
        Deny from all
    </Files>
    
    # 定義允許的 Referer (正則,NC=不分大小寫)
    # 空 Referer (直接訪問/書籤)
    RewriteCond %{HTTP_REFERER} ^$ [OR]
    # 本站 (含 www 子域名)
    RewriteCond %{HTTP_REFERER} ^https?://(www\.)?yourdomain\.com [NC,OR]
    # Google 圖片搜尋/網頁搜尋
    RewriteCond %{HTTP_REFERER} ^https?://(www\.)?google\.([a-z]{2,3}\.)?[a-z]{2,3} [NC,OR]
    # Baidu
    RewriteCond %{HTTP_REFERER} ^https?://(www\.)?baidu\.com [NC,OR]
    # Bing
    RewriteCond %{HTTP_REFERER} ^https?://(www\.)?bing\.com [NC,OR]
    # 其他常見搜尋/社群 (按需添加)
    RewriteCond %{HTTP_REFERER} ^https?://(www\.)?yahoo\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} ^https?://(www\.)?sogou\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} ^https?://(www\.)?so\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} ^https?://([a-z0-9-]+\.)?facebook\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} ^https?://([a-z0-9-]+\.)?twitter\.com [NC]
    
    # 符合上述任一條件 → 允許存取 (停止重寫)
    RewriteRule \.(gif|jpe?g|png|webp|bmp|svg)$ - [NC,L]
    
    # 其餘情況 → 重導向至替換圖片 (R=302 暫時重導, L=最後一條)
    # 注意:替換圖片**必須放在其他域名/路徑**,否則會無限迴圈!
    RewriteRule \.(gif|jpe?g|png|webp|bmp|svg)$ https://cdn.yourdomain.com/anti-hotlink.jpg [R,NC,L]
</IfModule>

關鍵細節解析

項目說明
RewriteCond ... [OR]多條件 **OR** 關係,任一滿足即通過
RewriteCond ... [NC]不分大小寫比對
RewriteRule ... - [L]- 表示不重寫 URL,[L] 停止處理 → 允許原圖輸出
替換圖片域名**不能是當前域名**,否則觸發防盜鏈規則 → 無限迴圈。建議用 CDN 域名/靜態資源域名/第三方圖床。
檔案類型根據實際需求增減副檔名 (如 webp、svg、ico)

進階:返回 403 而非重導向 (省流量、更嚴格)

# 移除最後一行 RewriteRule,改用:
RewriteRule \.(gif|jpe?g|png|webp|bmp|svg)$ - [F,NC,L]
# [F] = Forbidden (403),直接回傳錯誤頁面,不產生額外請求

Nginx 同樣邏輯配置

location ~* \.(gif|jpe?g|png|webp|bmp|svg)$ {
    valid_referers none blocked
        server_names
        *.google.* *.baidu.com *.bing.com *.yahoo.com *.sogou.com *.so.com
        *.facebook.com *.twitter.com;
    
    if ($invalid_referer) {
        # return 403;  # 或重導向
        rewrite ^ https://cdn.yourdomain.com/anti-hotlink.jpg redirect;
    }
}

驗證測試

  1. 瀏覽器直接開圖片網址 → 應正常顯示 (空 Referer)。
  2. 本站頁面嵌入圖片 → 正常顯示 (Referer 為本站)。
  3. 其他網站 `` → 應顯示替換圖片或 403。
  4. Google/Baidu 圖片搜尋 → 應正常收錄顯示 (Referer 含 google/baidu)。

部署步驟

  1. 建立 .htaccess 文字檔 (Windows 下先存為 htaccess.txt,上傳後改名)。
  2. 上傳至網站**根目錄** (或圖片所在目錄)。
  3. 確認 Apache 載入 mod_rewrite 且允許 AllowOverride All (或 FileInfo)。
  4. 上傳替換圖片 anti-hotlink.jpg 至**外部域名/CDN**。
  5. 清除瀏覽器快取測試。

總結

.htaccess 防盜鏈屬於**應用層防護**,對伺服器效能影響極小,配置靈活。核心在於:**白名單機制 (允許空 Referer + 本站 + 搜尋引擎) + 替換圖片放外部域名**。配合 CDN 防盜鏈 (如 Cloudflare、阿里雲 OSS Referer 白名單) 雙層防護效果最佳。

Copyright © Windows教程網 All Rights Reserved