在cakephp 2.0中保护图像/视频文件夹

dot*_*dot 5 .htaccess image hotlinking cakephp-2.0

我们目前正在使用webroot中的文件夹来存储我们正在视图中加载的图像和视频(使用通常的html图像帮助程序),这些图像和视频需要登录.

如何防止外部访问者只是使用site.com/img/photos/1.jpg网址并访问图像?根据我的理解,我无法真正使用媒体视图在正确的视图中呈现图像,我无法弄清楚是否有通过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>
Run Code Online (Sandbox Code Playgroud)

但重写基线似乎禁止访问整个网站,没有它,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>
Run Code Online (Sandbox Code Playgroud)

pon*_*cha 2

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

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !site.com
RewriteCond %{REQUEST_URI} ^img/
RewriteRule .* / [L,F]
Run Code Online (Sandbox Code Playgroud)

注意:如果有人想要窃取您的内容,很容易“破坏”这种保护,但是,它确实可以防止热链接,而无需生成某种脚本来传递所有图像/视频来检查是否应该访问的确。

编辑:

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

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

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

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

编辑2:

在你的例子中,整个集合应该是这样的:

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]
Run Code Online (Sandbox Code Playgroud)