WordPress使用.htaccess防盗链的方法

来自:互联网
时间:2018-10-16
阅读:

盗链是指其它网站直接使用你自己网站内的资源,从而浪费网站的流量和带宽,比如图片,上传的音乐,电影等文件。把下面的代码加入.htaccess中就能有效防盗链了。

# 防盗链
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteBase /wp-content/uploads/
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !inlojv.com [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteCond %{HTTP_REFERER} !bAIdu.com [NC]
RewriteCond %{HTTP_REFERER} !QQ.com [NC]
RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
RewriteCond %{HTTP_REFERER} !xianguo.com [NC]
RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC]
RewriteCond %{HTTP_REFERER} !yahoo.com [NC]
RewriteRule .*.(gif|jpg|jpeg|png|bmp|zip|rar|7z|swf|mp3)$ https://www.freexyz.cn/img/nolink.jpg [NC,L]
</IfModule>

其中 https://www.freexyz.cn/img/nolink.jpg 为防盗链图片提示。


下面解释一下上面添加代码的作用意思:

RewriteBase /wp-content/uploads/:设置WordPress附件上传目录禁止外链,如果你已经在后台更改了默认上传目录的,这里则相应修改。

RewriteCond %{HTTP_REFERER} !^$ [NC]:允许用户在浏览器中单独打开附件链接。

RewriteCond %{HTTP_REFERER} !inlojv.com [NC]:允许特定网站可以显示附件内容,一般为收录优化和方便订阅用户,建议加上Google、百度、Feedsky之类的允许显示的网站。

RewriteRule .*.(gif|jpg|jpeg|png|bmp|zip|rar|7z|swf|mp3)$ http://www.inlojv.com/wp-content/nolink.jpg [NC,L]:禁止外链的文件类型,如果盗链了则显示盗链提示图片。

返回顶部
顶部