本文介绍如何通过修改apache配置文件(通常为httpd.conf或apache2.conf)来启用防盗链功能。
步骤一:定位并打开apache配置文件
首先,找到并打开你的apache配置文件。文件位置通常为/etc/httpd/conf/(centos/rhel系统)或/etc/apache2/(debian/ubuntu系统)。使用以下命令打开:
sudo nano /etc/httpd/conf/httpd.conf # centos/rhel # 或 sudo nano /etc/apache2/apache2.conf # debian/ubuntu
步骤二:启用mod_rewrite模块
确保已启用mod_rewrite模块。这可以通过以下命令实现:
sudo a2enmod rewrite # debian/ubuntu # 或 sudo systemctl enable rewrite # centos/rhel
步骤三:添加防盗链规则
在apache配置文件的
方法一:使用.htaccess文件
在网站根目录创建或编辑.htaccess文件:
sudo nano /var/www/html/.htaccess
添加以下内容:
rewriteengine on rewritecond %{http_referer} !^http://(www\.)?yourdomain\.com [nc] rewritecond %{http_referer} !^$ rewriterule \.(jpg|jpeg|png|gif)$ - [f]
方法二:使用
在主配置文件中,在
<virtualhost *:80> servername yourdomain.com documentroot /var/www/html <directory /> rewriteengine on rewritecond %{http_referer} !^http://(www\.)?yourdomain\.com [nc] rewritecond %{http_referer} !^$ rewriterule \.(jpg|jpeg|png|gif)$ - [f] </directory> </virtualhost>
请将yourdomain.com替换为你的实际域名。
步骤四:重启apache服务器
保存配置文件后,重启apache服务器使更改生效:
sudo systemctl restart apache2 # debian/ubuntu # 或 sudo systemctl restart httpd # centos/rhel
步骤五:测试防盗链功能
尝试从其他域名访问你的图片,确认防盗链规则是否生效。如果配置正确,你将收到403 forbidden错误。
通过以上步骤,即可完成apache防盗链配置。 可根据实际需求调整规则以适应不同场景。
以上就是如何配置apache防盗链功能的详细内容,更多请关注代码网其它相关文章!
发表评论