【问题标题】:Protecting image/video folder in cakephp 2.0保护 cakephp 2.0 中的图像/视频文件夹
【发布时间】:2012-06-27 00:46:44
【问题描述】:

我们目前正在使用 webroot 中的文件夹来存储我们在需要登录的视图中加载的图像和视频(使用通常的 html 图像助手)。

如何防止外部访问者只使用site.com/img/photos/1.jpg url 并访问图像? 据我了解,我不能真正使用媒体视图在正确的视图中渲染图像,而且我不知道是否有通过 htaccess 操作的解决方案。

这方面的最佳做法是什么? 也许选择使用非 webroot 文件夹是最好的(尽管这会使文件存储部分变得更加困难)?

按照 poncha 的建议,我尝试将主 .htaccess 文件编辑到该文件中

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{HTTP_REFERER} !localhost
   RewriteCond %{REQUEST_URI} ^app/webroot/img/
   RewriteRule .* / [L,F]
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L] 
</IfModule>

但是重写基线似乎是禁止访问整个站点,没有它,img 访问似乎没有任何变化。

编辑 2: 在 webroot 中编辑 htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]

# this RewriteCond is needed to avoid rewrite loops
RewriteCond %{REQUEST_URI} !^/app/webroot/
RewriteRule (.*) app/webroot/$1 [L,R]


RewriteCond %{HTTP_REFERER} !127.0.0.1
RewriteCond %{REQUEST_URI} ^/app/webroot/img/
RewriteRule .* - [L,F]

</IfModule>

【问题讨论】:

    标签: image .htaccess cakephp-2.0 hotlinking


    【解决方案1】:

    这会检查 Referer http 标头是否设置为包含您的域的内容,如果没有则拒绝访问 img/ 文件夹。

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_REFERER} !site.com
    RewriteCond %{REQUEST_URI} ^img/
    RewriteRule .* / [L,F]
    

    注意:如果有人想窃取您的内容,“破坏”这种保护很容易,但是,它确实可以防止盗链,而无需产生某种形式将通过所有图像/视频以检查是否应授予访问权限的脚本。

    编辑:

    如果您的网站不在/,您有两种选择:

    1. 更改RewriteBase 以反映站点的基本URI(例如RewriteBase /app/webroot/

    2. 更改RewriteCond 以反映来自/ 的路径(例如RewriteCond ^app/webroot/img/

    第二个选项在您的情况下是首选,因为您还有其他规则

    EDIT2:

    在你的情况下,整个集合应该是这样的:

    RewriteEngine on
    RewriteBase /
    
    # this RewriteCond is needed to avoid rewrite loops
    RewriteCond %{REQUEST_URI} !^/app/webroot/
    RewriteRule (.*) app/webroot/$1 [L,R]
    
    RewriteCond %{HTTP_REFERER} !localhost
    RewriteCond %{REQUEST_URI} ^/app/webroot/img/
    RewriteRule .* - [L,F]
    

    【讨论】:

    • 我应该在 app 文件夹之外的主 htaccess 文件上应用它吗?另外,我目前正在 localhost/test 下的 localhost 上工作,这是我需要替换 site.com 的 url 吗?最后,这些行在哪里与预先存在的RewriteRule ^$ app/webroot/ [L] RewriteRule (.*) app/webroot/$1 [L] 结合在一起。谢谢
    • @nepo 1。这应该在您的 VirtualHost 配置中(在 httpd.conf 或每个 vhost 配置文件中 - 取决于分布)或在文档根目录中的 .htaccess 中(您的站点/到达的位置),2。是的,您的基本 uri(可能没有 http://,因为您可能使用 https)应该代替 site.com3。他们应该在那些预先存在的行之前去
    • 嗯,RewriteBase / 行似乎禁止访问整个站点,但奇怪的是我仍然可以使用 http://localhost/test/app/webroot/img 访问图像文件夹
    • 如果您的网站不在/ 中,那么RewriteBase 也应该在那里...或者在RewriteCond 中包含路径。您在原始帖子中提到您要保护的网址格式为 site.com/img/photos/1.jpg
    • 我已经将我的站点放在 localhost 而不是 localhost/test 上,以使其可能稍微容易一些,但该文件夹仍然可见。请注意,预先存在的重写规则将 localhost/img/ 转换为 localhost/app/webroot/img (您可能已经了解我没有使用 htaccess 编辑的经验,所以我不确定这是否有帮助)检查我的编辑以查看当前.htaccess 的形式
    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 2023-04-02
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多