(htaccess的)如何prevent直接的URL访问的文件? [英] (htaccess) How to prevent a file from DIRECT URL ACCESS?

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

问题描述

这是我的第二个职位,同样的问题。怎么把previous后需要一些更多的例子。

确定可以再说一遍,我使用Apache和我有一个简单的Web文件夹(在我的本地),如:

Ok lets say again, i'm using Apache and i have a sample web folder (on my Localhost) like:

在文件夹中,该文件将是:

In the folder, the files will be:

  • index.html的
  • sample.jpg
  • 的.htaccess

在的index.html将只包括了:

The "index.html" will simply include:

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

当我运行的网站的http://本地主机/测试/ ,会有简单的显示页面上的图像`sample.jpg

When i run the website at http://localhost/test/, there will be simply showing the image `sample.jpg' on the page.

那么这里的问题是

  • 我想prevent图像显示为的http:直接在地址栏//localhost/test/sample.jpg

注:
1天得到下面的回答后,我发现所有的解决方案在逻辑上工作,但问题与Firefox。我的意思是,下面的答案给予解决方案,同时测试每个浏览器,但无法与Firefox浏览器。

Note:
After one day of getting below answers, i found all solutions logically work, but problem with Firefox. I mean, the below answers are giving the solution while testing on every browser but not with 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 image directly, but allows them to be displayed on site.

请注意:这是可能的,当你打开一些网页,图像,比复制图像的路径到地址栏就可以看到图像,它只是浏览器的缓存,因为,事实上该图像尚未加载来自服务器(从DAVO,低于全注释)。

NOTE: it is possible that when you open some page with image and than 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).

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

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