在Ubuntu上配置Apache防盗链策略可以通过多种方法实现,以下是两种常见的方法:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain\.com[NC]
RewriteRule \.(gif|jpg|jpeg|png)$ http://www.yourdomain.com/404.html [R=301,L]
将yourdomain.com
替换为你的实际域名。
mod_rewrite
模块已启用。可以通过在终端运行以下命令来启用它:sudo a2enmod rewrite
/etc/apache2/sites-available/
目录下。例如,编辑默认的虚拟主机配置文件:sudo nano /etc/apache2/sites-available/000-default.conf
<VirtualHost>
块内添加以下代码:<Directory "/path/to/your/website">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain\.com[NC]
RewriteRule \.(gif|jpg|jpeg|png)$ http://www.yourdomain.com/404.html [R=301,L]
</Directory>
将/path/to/your/website
替换为你的网站路径,将yourdomain.com
替换为你的实际域名。
sudo systemctl restart apache2
以上两种方法都可以有效地防止其他网站盗用你的静态资源,如图片、视频等,从而保护你的服务器带宽。