在 CentOS 系統中,使用 Apache2 實現防盜鏈的方法有很多種,這里介紹一種常用的方法:使用 mod_rewrite 模塊實現防盜鏈。
sudo yum install httpd
sudo systemctl start httpd
cd /var/www/html/images
touch .htaccess
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain\.com [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpg|jpeg|png|gif)$ - [F,L]
將 “yourdomain.com” 替換為你自己的域名。這段代碼的意思是:如果訪問者請求的資源不是來自 “yourdomain.com”,則拒絕訪問。
保存 .htaccess 文件并關閉文本編輯器。
重啟 Apache2 服務以使更改生效:
sudo systemctl restart httpd
現在,已經成功為指定目錄實現了防盜鏈功能。當其他網站嘗試引用該目錄下的資源時,將會收到 403 Forbidden 錯誤。