如何防止文件直接访问URL?

时间:2012-04-19 21:02:46

标签: apache .htaccess mod-rewrite hotlinking

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

      http://localhost/test/

test文件夹中的文件:

     index.html  
     sample.jpg  
     .htaccess  

index.html的示例来源:

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

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


问题:

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

注意: 我发现以下解决方案在每个浏览器上进行测试除了Firefox

7 个答案:

答案 0 :(得分:96)

尝试以下方法:

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

如果直接访问图像,则返回403,但允许它们在现场显示。

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

答案 1 :(得分:22)

罗西波夫的规则很有效!

我在实际网站上使用它来显示空白或特殊消息;)代替对文件的直接访问尝试我宁愿保护一点不被直接查看。我觉得它比403 Forbidden更有趣。

因此,采用rosipov规则将任何直接请求重定向到{gif,jpg,js,txt}文件为'messageforcurious':

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.ltd [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.ltd.*$ [NC] 
RewriteRule \.(gif|jpg|js|txt)$ /messageforcurious [L]

我认为这是一种礼貌的方式,不允许直接访问,例如,CMS 敏感文件,如xml,javascript ...考虑到安全性:对于所有这些机器人现在潦草地写入网页,我想知道他们的算法会从我的'信息'中发出什么。

答案 2 :(得分:8)

首先,找到主apache的配置文件httpd.conf所在的位置。如果你使用Debian,它应该在这里:/etc/apache/httpd.conf。使用像Vim或Nano这样的文件编辑器打开这个文件,找到如下所示的行:

Options Includes Indexes FollowSymLinks MultiViews

然后删除单词索引并保存文件。该行应如下所示:

Options Includes FollowSymLinks MultiViews

完成后,重新启动apache(例如在Debian中重启/etc/init.d/apache)。而已!

答案 3 :(得分:7)

根据您的评论看起来这就是您所需要的:

RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost/ [NC] 
RewriteRule \.(jpe?g|gif|bmp|png)$ - [F,NC]

我已经在我的localhost上测试了它,似乎工作正常。

答案 4 :(得分:0)

在Web服务器上使用它时,只能重命名本地主机,如下所示:

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

答案 5 :(得分:0)

from tkinter import *

def window():

    def sign_up_clicked():
        return True
        print ('test')

    def log_in_clicked():
        return False
        print ('test')

    window = Tk()
    window.title('Welcome')
    window.geometry('480x200')

    welcome_lbl = Label(window, text='Welcome', font=('Arial', 26), fg='#71C2FE') #Label with text saying 'welcome' in light blue 
    welcome_lbl.place(x=15, y=10) #placing welcome label in top left of sign_up_window

    under_line_lbl = Label(window, text='_______________________', font=('Arial', 35), fg='#FFB56B') #orange line under blue for looks
    under_line_lbl.place(x=0, y=40)

    intro_lbl = Label(window, text='Please press login if you already have an account,\notherwiseyou can create an account by pressing sign-up. ', font=('Arial', 14, 'bold italic'), justify='left')#lable with text underlined and italics
    intro_lbl.place(x=10, y=90)

    sign_up_btn = Button(window, text='Sign up', relief='raised', width=6, font=('candara', 14), command=sign_up_clicked)
    sign_up_btn.place(x=10, y=130)

    log_in_btn = Button(window, text='Login', relief='raised', width=6, font=('candara', 14), command=log_in_clicked)
    log_in_btn.place(x=10, y=160)

    window.mainloop()

window()

答案 6 :(得分:0)

对我来说,这是唯一有效的方法,并且效果很好:

RewriteCond%{HTTP_HOST} @@%{HTTP_REFERER}!^([^ @] )@@ https?:// \ 1 /。
RewriteRule。(gif | jpg | jpeg | png | tif | pdf | wav | wmv | wma | avi | mov | mp4 | m4v | mp3 | zip?)$-[F]

在以下位置找到了它: https://simplefilelist.com/how-can-i-prevent-direct-url-access-to-my-files-from-outside-my-website/

相关问题