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

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

问题描述

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

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

      http://localhost/test/

<小时>

test 文件夹中的文件:

     index.html  
     sample.jpg  
     .htaccess  

<小时>

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.

问题:

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

注意:我发现以下解决方案在每个浏览器上测试时都有效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天全站免登陆