简体   繁体   English

阻止图像文件直接访问 URL?

[英]Prevent an image file from DIRECT URL ACCESS?

I want to prevent people from getting the images of my website by typing in the URLs in browser address bar, while allowing them to view the images on when visiting the webpages.我想阻止人们通过在浏览器地址栏中输入 URL 来获取我网站的图像,同时允许他们在访问网页时查看图像。

I tried the following .htaccess code:我尝试了以下 .htaccess 代码:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC]
RewriteRule \.(gif|jpg|png)$ - [F]

However, it not only restricts direct URL access, but also hides all of images even when visiting the webpage.但是,它不仅限制了URL的直接访问,而且在访问网页时也会隐藏所有图片。 Now my website looks like a page of text with a lot of image holes.现在我的网站看起来像一个带有很多图像孔的文本页面。

Can anyone tell me why the above .htaccess doesn't work?谁能告诉我为什么上面的 .htaccess 不起作用?

I found it here .htaccess) How to prevent a file from DIRECT URL ACCESS?我在这里找到它.htaccess)如何防止文件直接访问 URL? , but I don't have enough reputation to ask in that post, because I just created this account to post this question. ,但我没有足够的声望在那篇文章中提问,因为我刚刚创建了这个帐户来发布这个问题。

Thanks.谢谢。

Returns 403 (Forbidden) if you access image directly, but allows them to be displayed on site. 如果直接访问图像,则返回403(禁止),但允许将其显示在网站上。 It works, but testing can be tricky/misleading. 它可以工作,但是测试可能很棘手/误导。 Read about it in questions/10236717/htaccess-how-to-prevent-a-file-from-direct-url-access 请在问题中查看有关此内容的信息/ 10236717 / htaccess-如何防止来自直接URL访问的文件

Another way to prevent hotlinking is like this (from: htaccess generators 防止热链接的另一种方式是这样的(来自: htaccess generators

RewriteEngine on  
RewriteCond %{HTTP_REFERER} !^$  
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]  
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

you can simply type Option -Indexes in the .htaccess file and this will prevent direct URL access to the image file您只需在 .htaccess 文件中键入 Option -Indexes,这将阻止 URL 直接访问图像文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM