使用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

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

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
