Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何防止文件直接访问URL?_Regex_Apache_.htaccess_Mod Rewrite_Hotlinking - Fatal编程技术网

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

Regex 如何防止文件直接访问URL?,regex,apache,.htaccess,mod-rewrite,hotlinking,Regex,Apache,.htaccess,Mod Rewrite,Hotlinking,我正在使用Apache,在本地主机上有一个示例web文件夹,如: http://localhost/test/ test文件夹中的文件: index.html sample.jpg .htaccess index.html的示例源: <html> <body> <img src="sample.jpg" /> </body> </html> 当我在htt

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

      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
    直接在url栏中

注意:我发现下面的解决方案在除Firefox之外的所有浏览器上都能正常运行。根据您的评论,这就是您需要的:

RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost/ [NC] 
RewriteRule \.(jpe?g|gif|bmp|png)$ - [F,NC]
我已经在本地主机上测试了它,它似乎工作正常。

请尝试以下操作:

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC] 
RewriteRule \.(gif|jpg)$ - [F]
如果您直接访问图像,但允许在站点上显示图像,则返回403


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

首先,查找apache主配置文件httpd.conf的位置。如果您使用Debian,它应该在这里:
/etc/apache/httpd.conf
。使用一些文件编辑器(如Vim或Nano)打开此文件,并找到如下所示的行:

Options Includes Indexes FollowSymLinks MultiViews
然后删除word索引并保存文件。这条线应该是这样的:

Options Includes FollowSymLinks MultiViews

完成后,重新启动apache(例如,Debian中的/etc/init.d/apache restart)。就这样

罗西波夫的规则非常有效

我在实时站点上使用它来显示空白或特殊消息;)为了避免直接访问文件,我宁愿保护一点不被直接查看。我觉得这比403更有趣

因此,根据罗西波夫的规则,将任何直接请求重定向到{gif,jpg,js,txt}文件的请求重定向到'messageforquirier':

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。。。考虑到安全性:对于现在所有这些在网上涂鸦的机器人,我想知道他们的算法会从我的“MessageForGuardy”中得到什么。

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

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

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

重写条件%{HTTP\u HOST}@@@%{HTTP\u REFERER}^([^@])@@https?://\1/
重写规则(gif | jpg | jpeg | png | tif | pdf | wav | wmv | wma | avi | mov | mp4 | m4v | mp3 | zip?$-[F]

在以下网址找到:

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