在Apache服务器中,可以通过配置.htaccess文件或直接修改httpd.conf文件来实现防盗链功能。以下是两种方法的详细步骤:
.htaccess文件找到或创建.htaccess文件:
编辑.htaccess文件:
.htaccess文件,添加以下内容:RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain\.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F,L]
RewriteEngine On:启用URL重写引擎。RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain\.com [NC]:检查请求的Referer是否不是你的域名(不区分大小写)。RewriteRule \.(jpg|jpeg|png|gif)$ - [F,L]:如果Referer不符合条件,则返回403 Forbidden状态码,并停止进一步处理(L标志)。httpd.conf或apache2.conf文件找到Apache配置文件:
httpd.conf或apache2.conf,具体路径取决于你的操作系统和Apache安装方式。编辑配置文件:
<Directory "/path/to/your/website">
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain\.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F,L]
</Directory>
<Directory "/path/to/your/website">:指定要应用规则的目录路径。.htaccess文件中的配置相同。保存并关闭文件:
重启Apache服务器:
sudo systemctl restart apache2
通过以上方法,你可以在Apache服务器中成功设置防盗链功能。