使用Apache的htaccess防止图片被盗链的解决方法_PHP
Apache
方法一:
所需要的命令如下:
复制代码 代码如下:
RewriteEngine on
RewriteCond %{ HTTP_REFERER } !^$
RewriteCond %{ HTTP_REFERER } !^http://(www.)?simue.com /.*$ [NC]
RewriteRule .(gif &line;jpg)$ - [F]
如果觉得让别人的页面开个天窗不好看,那可以用一张图片来代替:
查看代码
复制代码 代码如下:
RewriteEngine on
RewriteCond %{ HTTP_REFERER } !^$
RewriteCond %{ HTTP_REFERER } !^http://(www.)?simue.com/.*$ [NC]
RewriteRule .(gif &line;jpg)$ http://www.simue.com /替代图片文件名 [R,L]
需要注意的是,如果通过FTP方式将创建好的.htaccess上传到服务器上,传输模式应为ASCII而非Binary。上传到服务器后,应将其属性通过 CHMOD修改为644 或“RW-R–R–”,这样,可以保证服务器能够使用同时无法通过浏览器修改,当然,.htaccess的可读属性也存在一定的风险:攻击者可通过它找出 您要保护的对象或认证文件位置——解决办法是将认证文件.htpasswd放到网站根目录之外,这样,便无法通过网络找到它了。
方法二:
复制代码 代码如下:
查看代码01 RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !simue.com [NC]
RewriteCond %{HTTP_REFERER} !baidu.com [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteRule .*\.(gif|jpg)$ http://www.simue.com /no.png [R,NC,L]
简单的解释一下上述语句:
1、RewriteCond %{HTTP_REFERER} !^$ [NC]
允许空“HTTP_REFERER”的访问,即允许用户在浏览器地址栏中直接输入图片地址时图片文件的显示。一般而言,这是可选的,不过,建议这么设置,如果强迫必须具有“HTTP_REFERER”才能访问,可能会带来某些问题,比如说在用户通过代理服务器访问时。
2、RewriteCond %{HTTP_REFERER} !simue.com [NC]
设置允许访问的HTTP来源,包括我们的网站 自身、Google、Baidu等。
3、RewriteRule .*\.(gif|jpg|png)$ http://www.simue.com/no.png [R,NC,L]
定义被盗链时替代的图片,让所有盗链 jpg、gif、png 等文件的网页,显示根目录下的 no.png 文件。注意:替换显示的图片不要放在设置防盗链的目录中,并且该图片文件体积越小越好。当然你也可以不设置替换图片,而是使用下面的语句即可:
RewriteRule .*\.(gif|jpg|png)$ - [F]
4、说明一下其中的R、NC 和 L
R 就是转向的意思
NC 指的是不区分大小写
L 的作用是指明本次转向到此结束,后续的转向不受先前判断语句的影响
5、防止盗链的文件类型
上例中是 gif、jpg、png,而根据需要,可更改或添加其他文件类型,如rar、swf等,不同文件扩展名间使用“|”分割。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...
