如何利用.htaccess防盗链网站图片
防盗链网站图片顾名思义就是防止外部网站盗用我们网站的图片,它的作用就是防止其他人在其他的博客,空间网站里面贴上我们网站的图片从而导致访问者们没有进入我们的网站,但是还是耗费了我们网站空间的流量。这是不是非常的不爽?好了,下面我们就来看看如何利用.htaccess来防止别人盗链我们网站的图片吧!
利用.htaccess来防盗链图片其实很简单,只要你懂得.htaccess规则,也就一句话的事儿。下面我们来看一个完整的例子:
RewriteEngine On RewriteBase / RewriteCond %{HTTP_REFERER} !^$ [NC] RewriteCond %{HTTP_REFERER} !phpernote.com [NC] RewriteCond %{HTTP_REFERER} !google.com [NC] RewriteCond %{HTTP_REFERER} !baidu.com [NC] RewriteCond %{HTTP_REFERER} !sogou.com [NC] RewriteCond %{HTTP_REFERER} !soso.com [NC] RewriteCond %{HTTP_REFERER} !youdao.com [NC] RewriteCond %{HTTP_REFERER} !yahoo.cn [NC] RewriteRule ^uploadfiles/(.*)\.(gif|jpg|png)$ http://www.phpernote.com/images/loading.gif [L]
下面对以上这段代码做一下详细的解释:
RewriteEngine On
url重写引擎开关,如果设为off,则任何重写规则定义将不被应用,该开关的另一好处就是如果为了临时拿掉重写规则,则改为off再重启动Apache即可,不必将下面一条条的重写规则注释掉。注意这条语句尽量写在文件的头一行。
RewriteBase /
这条语句的作用是如果在下面的rewriterule定义中被重写后的部分(此处为文件名index.html)。如果前面没有/,则是相对目录。一般直接设置为 / 意思就是根目录。
允许通过空的“HTTP_REFERER”的访问,即在用户在浏览器地址栏中直接输入图片地址的情况下图片是可以显示的。一般而言,这是可选的,不过,建议做如上设置,否则如果强迫必须具有“HTTP_REFERER”才能访问的话,可能会带来某些问题,比如说在用户通过代理服务器访问时。 RewriteCond %{HTTP_REFERER} !phpernote.com [NC] 这里设置的是允许访问的HTTP来源,这里包括我们自己的网站、google.com、baidu.com、sogou.com、soso.com、youdao.com、yahoo.cn。这也是为了让搜索引擎能正常的抓取我们的网站图片做考虑。 RewriteRule .*\.(gif|jpg|png|bmp)$ http://www.phpernote.com/change.gif [R,NC,L] OK,这里就是设置防盗链的规则了。这里的设置是让所有盗链本网站的 jpg、gif、png、bmp 等图片文件的网页,一律显示以 http://www.phpernote.com/change.gif 这个文件替代显示。特别需要注意的是:替换显示的图片不要放在设置防盗链的目录中或者包含在设置了防盗链的目录中,并且该图片文件体积越小越好。当然你也可以不设置替换图片,使用如下这样的规则,盗链的网站的图片将显示一篇空白 RewriteRule .*\.(gif|jpg|png|bmp)$ – [F] 另外如果你不明白上面的规则后面的中括号里面的 NC F L 的字符含义的话,请参照本站:.htaccess 语法参数说明 好了,经过以上的设置,所有盗链你网站的图片文件的网站,都将会因为这种不友好的显示而放弃盗链了,这在一定程度上减少了你的空间服务器不必要的流量,是一件值得去做的事情。
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteCond %{HTTP_REFERER} !baidu.com [NC]
RewriteCond %{HTTP_REFERER} !sogou.com [NC]
RewriteCond %{HTTP_REFERER} !soso.com [NC]
RewriteCond %{HTTP_REFERER} !youdao.com [NC]
RewriteCond %{HTTP_REFERER} !yahoo.cn [NC]
您可能感兴趣的文章
- .htaccess如何设置防盗链某个目录的图片
- 利用.htaccess拒绝某ip访问网站
- 使用Apache的htaccess防止图片被盗链
- 利用.htaccess禁止列表目录
- 10 段实用的 .htaccess 代码片段
- 通过.htaccess设置网站默认首页
- 如何通过.htaccess修改网站默认首页?
- php利用curl实现多线程的类,php curl多线程下载图片

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

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

With the popularity of Douyin short videos, user interactions in the comment area have become more colorful. Some users wish to share images in comments to better express their opinions or emotions. So, how to post pictures in TikTok comments? This article will answer this question in detail and provide you with some related tips and precautions. 1. How to post pictures in Douyin comments? 1. Open Douyin: First, you need to open Douyin APP and log in to your account. 2. Find the comment area: When browsing or posting a short video, find the place where you want to comment and click the "Comment" button. 3. Enter your comment content: Enter your comment content in the comment area. 4. Choose to send a picture: In the interface for entering comment content, you will see a "picture" button or a "+" button, click

Apple's recent iPhones capture memories with crisp detail, saturation and brightness. But sometimes, you may encounter some issues that may cause the image to look less clear. While autofocus on iPhone cameras has come a long way, allowing you to take photos quickly, the camera can mistakenly focus on the wrong subject in certain situations, making the photo blurry in unwanted areas. If your photos on your iPhone look out of focus or lack sharpness overall, the following post should help you make them sharper. How to Make Pictures Clearer on iPhone [6 Methods] You can try using the native Photos app to clean up your photos. If you want more features and options

In PowerPoint, it is a common technique to display pictures one by one, which can be achieved by setting animation effects. This guide details the steps to implement this technique, including basic setup, image insertion, adding animation, and adjusting animation order and timing. Additionally, advanced settings and adjustments are provided, such as using triggers, adjusting animation speed and order, and previewing animation effects. By following these steps and tips, users can easily set up pictures to appear one after another in PowerPoint, thereby enhancing the visual impact of the presentation and grabbing the attention of the audience.

Are you also using Foxit PDF Reader software? So do you know how Foxit PDF Reader converts pdf documents into jpg images? The following article brings you how Foxit PDF Reader converts pdf documents into jpg images. For those who are interested in the method of converting jpg images, please come and take a look below. First start Foxit PDF Reader, then find "Features" on the top toolbar, and then select the "PDF to Others" function. Next, open a web page called "Foxit PDF Online Conversion". Click the "Login" button on the upper right side of the page to log in, and then turn on the "PDF to Image" function. Then click the upload button and add the pdf file you want to convert into an image. After adding it, click "Start Conversion"

How to use JavaScript to implement the drag and zoom function of images? In modern web development, dragging and zooming images is a common requirement. By using JavaScript, we can easily add dragging and zooming functions to images to provide a better user experience. In this article, we will introduce how to use JavaScript to implement this function, with specific code examples. HTML structure First, we need a basic HTML structure to display pictures and add

When using WPS office software, we found that not only one form is used, tables and pictures can be added to the text, pictures can also be added to the table, etc. These are all used together to make the content of the entire document look richer. , if you need to insert two pictures into the document and they need to be arranged side by side. Our next course can solve this problem: how to place two pictures side by side in a wps document. 1. First, you need to open the WPS software and find the picture you want to adjust. Left-click the picture and a menu bar will pop up, select "Page Layout". 2. Select "Tight wrapping" in text wrapping. 3. After all the pictures you need are confirmed to be set to "Tight text wrapping", you can drag the pictures to the appropriate position and click on the first picture.

Overview of advanced functions of how to use HTML, CSS and jQuery to implement image merge display: In web design, image display is an important link, and image merge display is one of the common techniques to improve page loading speed and enhance user experience. This article will introduce how to use HTML, CSS and jQuery to implement advanced functions of image merging and display, and provide specific code examples. 1. HTML layout: First, we need to create a container in HTML to display the merged images. You can use di
