search
Homephp教程php手册Apache的mod_rewrite模块拒绝盗链

网站上传了一首mp3做背景音乐,不久,网站流量突然暴增,有几次甚至导致了VPS的当机。后来经过分析:盗链这个MP3的网页包括诸如校内网空间、QQ空间、更有甚者还放到了Taobao小店、个人博客也有不少,全部作为了背景音乐,并且导致各种爬虫疯狂抓取这个文件。找到了问题原因就只有一个办法了,就是利用Apache的mod_rewrite模块把盗链行为拒之门外。

网站上传了一首mp3做背景音乐,不久,网站流量突然暴增,有几次甚至导致了VPS的当机。后来经过分析:盗链这个MP3的网页包括诸如校内网空间、QQ空间、更有甚者还放到了Taobao小店、个人博客也有不少,全部作为了背景音乐,并且导致各种爬虫疯狂抓取这个文件。找到了问题原因就只有一个办法了,就是利用Apache的mod_rewrite模块把盗链行为拒之门外。

利用.htaccess文件,设置图片mp3歌曲等的防盗链:

前提Apache中的mod_rewrite模块为激活状态,既然要消灭掉mp3文件的盗链就顺便也把图片盗链的设置给做了,下面是我在网站根目录的.htaccess文件中的配置,供同病相怜之人参考:

.htaccess文件中的代码:

以下为引用的内容:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www.)?cpanel123.com/.*$ [NC]
RewriteRule .(txtdocjpggifmp3ziprarpng)$ http://www.cpanel123.com [R=301,L]


现在盗链的话,就会自动转向到我的主页。

功能:

不仅屏蔽/禁止非本网站的盗链访问,还将盗链请求转给自身的网站,让读者知道谁是真正的原创作者。

代码说明:

RewriteCond 条件:意思就是所有的mp3、rar、图片等,访问如果referer不是本网站cpanel123.com或www.cpanel123.com,

RewriteRule 规则:自动转向到本站主页: http://www.cpanel123.com/

注释掉的部分是测试rewrite engine用的:对于调试mod_rewrite很有用。

其它防盗链的一些设置,大家可以参考:

以下为引用的内容:
#开启mod_rewrite引擎
RewriteEngine On
#设置合法请求源
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !zhangzhang.net [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteCond %{HTTP_REFERER} !baidu.com [NC]
RewriteCond %{HTTP_REFERER} !yahoo.com [NC]
RewriteCond %{HTTP_REFERER} !msn.com [NC]
RewriteCond %{HTTP_REFERER} !bloglines.com [NC]
RewriteCond %{HTTP_REFERER} !feedburner.com [NC]
RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
#指向警告页面
RewriteRule .*\.(gifjpgpngmp3)$ outlink.gif [L,NC]



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool