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

Apache的mod_rewrite模块拒绝盗链

Jun 21, 2016 am 08:57 AM
apache

网站上传了一首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 of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

How to export pdf with xml How to export pdf with xml Apr 03, 2025 am 06:45 AM

There are two ways to export XML to PDF: using XSLT and using XML data binding libraries. XSLT: Create an XSLT stylesheet, specify the PDF format to convert XML data using the XSLT processor. XML Data binding library: Import XML Data binding library Create PDF Document object loading XML data export PDF files. Which method is better for PDF files depends on the requirements. XSLT provides flexibility, while the data binding library is simple to implement; for simple conversions, the data binding library is better, and for complex conversions, XSLT is more suitable.

How does Apache or Nginx work together with PHP: What is the difference between mod_php5, php-cgi and php-fpm? How does Apache or Nginx work together with PHP: What is the difference between mod_php5, php-cgi and php-fpm? Apr 01, 2025 pm 12:15 PM

The collaborative working mechanism between Apache or Nginx and PHP: Comparison of mod_php5, php-cgi and php-fpm is to use Apache or Nginx to build a web server and use PHP for backend...

How is Debian Hadoop compatibility How is Debian Hadoop compatibility Apr 02, 2025 am 08:42 AM

DebianLinux is known for its stability and security and is widely used in server, development and desktop environments. While there is currently a lack of official instructions on direct compatibility with Debian and Hadoop, this article will guide you on how to deploy Hadoop on your Debian system. Debian system requirements: Before starting Hadoop configuration, please make sure that your Debian system meets the minimum operating requirements of Hadoop, which includes installing the necessary Java Runtime Environment (JRE) and Hadoop packages. Hadoop deployment steps: Download and unzip Hadoop: Download the Hadoop version you need from the official ApacheHadoop website and solve it

Apache Troubleshooting: Diagnosing & Resolving Common Errors Apache Troubleshooting: Diagnosing & Resolving Common Errors Apr 03, 2025 am 12:07 AM

Apache errors can be diagnosed and resolved by viewing log files. 1) View the error.log file, 2) Use the grep command to filter errors in specific domain names, 3) Clean the log files regularly and optimize the configuration, 4) Use monitoring tools to monitor and alert in real time. Through these steps, Apache errors can be effectively diagnosed and resolved.

Is Debian Strings compatible with multiple browsers Is Debian Strings compatible with multiple browsers Apr 02, 2025 am 08:30 AM

"DebianStrings" is not a standard term, and its specific meaning is still unclear. This article cannot directly comment on its browser compatibility. However, if "DebianStrings" refers to a web application running on a Debian system, its browser compatibility depends on the technical architecture of the application itself. Most modern web applications are committed to cross-browser compatibility. This relies on following web standards and using well-compatible front-end technologies (such as HTML, CSS, JavaScript) and back-end technologies (such as PHP, Python, Node.js, etc.). To ensure that the application is compatible with multiple browsers, developers often need to conduct cross-browser testing and use responsiveness

What are the key information in the Debian logs that cannot be ignored What are the key information in the Debian logs that cannot be ignored Apr 02, 2025 am 08:12 AM

The log files of the Debian system are valuable resources for system administrators and developers to diagnose problems and monitor the operating status of the system. This article will focus on some key log information that cannot be ignored. Core system logs (usually located in /var/log/syslog or /var/log/messages) These logs record the core activities of the system, including: system startup and shutdown events: log kernel version, hardware detection results, etc., to help track startup failures or shutdown exceptions. Hardware failure alerts: such as disk errors, memory problems, etc., to promptly detect potential hardware problems. Service status changes: Record the service start, stop and restart events to facilitate monitoring of the service's health. User login/logout history:

See all articles