如何防止文件直接访问URL? [英] How to prevent a file from direct URL Access?

查看:250
本文介绍了如何防止文件直接访问URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache,并且在本地主机上有一个示例Web文件夹,例如:

I'm using Apache and I have a sample web folder on my Local Host, like:

      http://localhost/test/


test文件夹中的文件:


Files in the test folder:

     index.html  
     sample.jpg  
     .htaccess  


index.html的示例来源:


Sample source of index.html:

<html>
  <body>
    <img src="sample.jpg" />
  </body>
</html>

当我在http://localhost/test/上运行网站时,它只会在页面上显示图像"sample.jpg".

When I run the website at http://localhost/test/, it will simply show the image `sample.jpg' on the page.

问题:

  • 我想防止图像直接在网址栏中显示为http://localhost/test/sample.jpg.

注意: 我发现,在除Firefox 以外的所有浏览器上测试以下解决方案均有效.

Note: I found that the solutions below work when tested on every browser except Firefox.

推荐答案

尝试以下操作:

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

如果直接访问图像,则返回403,但允许它们在网站上显示.

Returns 403, if you access images directly, but allows them to be displayed on site.

注意:当您打开带有图像的页面,然后将该图像的路径复制到地址栏中时,您可能会看到该图像,这可能仅是由于浏览器的缓存,实际上该图片尚未从服务器(来自Davo,下面有完整评论)中加载.

Note: It is possible that when you open some page with image and then copy that image's path into the address bar you can see that image, it is only because of the browser's cache, in fact that image has not been loaded from the server (from Davo, full comment below).

这篇关于如何防止文件直接访问URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆