【问题标题】:Mod_Rewrite conditions help for hotlinking but allow local requestsMod_Rewrite 条件有助于盗链,但允许本地请求
【发布时间】:2012-09-18 05:21:55
【问题描述】:

我有热链接保护工作,所以我想,但不适用于当引荐来源是本地时。

IndexIgnore *
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10

RewriteCond %{REQUEST_URI} ^/image_dir/
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?domain.com/
RewriteRule ^(.*)$ /some_dir/some_file.php?querystring=$1 [L]

我需要的是:

如果在网站上的任何图像请求中,引荐来源不是来自我的网站,则重定向到包含用户请求的图像的页面。

如果引用者来自我的站点,并且请求是针对 image_dir 中的任何图像,则重定向,否则不要。

非常感谢任何帮助!

编辑:这是帮助的完整文件:

## Default .htaccess file
IndexIgnore *
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10

# your rules were looping before, that's bad, this will stop it
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

# redirect the browser back to the referring page
RewriteCond %{REQUEST_URI} ^/img/
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?domain.com/
RewriteRule ^ %{HTTP_REFERER} [L,R]

# rewrite the request to the handler
RewriteCond %{REQUEST_URI} ^/img/
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g)$ [NC]
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?domain.com/
RewriteRule ^(.*)$ /img/index.php?img_hash=$1 [L]


## Do pretty permalinks for our images
# these rules ensure you aren't clobbering legit requests (like to image.php) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# just in case, exclude the admin directory 
RewriteCond %{REQUEST_URI} !^/admin [NC] 

# rewrite 
RewriteRule ^(.*)$ /display_image.php?img_hash=$1 [L] 

除了盗链之外,一切都如我所愿:

domain.com/t4hd -> domain.com/display_image.php?img_hash=t4hd (WORKS)
domain.com/t4hd.jpg -> domain.com/display_image.php?img_hash=t4hd.jpg (WORKS)
domain.com/img/t4hd -> domain.com/img/index.php?img_hash=t4hd (WORKS)
domain.com/img/t4hd.jpg -> domain.com/img/index.php?img_hash=t4hd.jpg (WORKS)
domain.com/img/123/456/789/t4hd.jpg -> domain.com/img/index.php?img_hash=t4hd.jpg (DOES NOT WORK)

我仍然必须允许所有请求:

domain.com/admin/index.php (WORKS)
domain.com/profile/index.php (WORKS)

希望这有助于澄清正在发生的事情。很抱歉一开始没有这样做。

【问题讨论】:

  • 其他.htaccess 指令还有效吗?是RewriteEngine On,因为你还没有将它包含在你的sn-p中吗?
  • 我已经更新了帖子以提供更多信息。
  • “但当引荐来源地是本地时不适用”是什么意思?你的意思是你不希望你的访问者看到你的图像而不看到它嵌入到你的页面中?任何人都不能直接访问“image_dir”目录?

标签: regex .htaccess mod-rewrite


【解决方案1】:

尝试使用这些规则:

# your rules were looping before, that's bad, this will stop it
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

# redirect the browser back to the referring page
RewriteCond %{REQUEST_URI} ^/image_dir/
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?domain.com/
RewriteCond %{HTTP_REFERER} !^$
RewriteRule ^ %{HTTP_REFERER} [L,R]

# rewrite the request to the handler
RewriteCond %{REQUEST_URI} ^/image_dir/
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g)$ [NC]
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?domain.com/ [OR]
RewriteCond %{HTTP_REFERER} ^$
RewriteRule ^(.*)$ /some_dir/some_file.php?querystring=$1 [L]

也就是说,我在你的问题中假设了这个神秘的部分:

如果引用者来自我的站点,并且请求是针对 image_dir 中的任何图像,则重定向,否则不要。

意思是:

“如果引用者来自我的站点并且请求是针对 image_dir 中的任何图像,则将请求发送到 some_file.php 处理程序,否则不要执行任何操作。”

【讨论】:

  • @user1666691 domain.com/img/123/456/789/t4hd.jpg -> domain.com/img/index.php?img_hash=t4hd.jpg (DOES NOT WORK) 的描述性不是很好,甚至不知道在这种情况下“有效”或“无效”是什么意思。
  • 对不起,我的意思是如果你尝试 domain.com/img/123/456/789/t4hd.jpg 它应该重定向到 domain.com/img/index.php?img_hash=t4hd.jpg它目前没有。它目前将您发送至domain.com/var/www/domain.com/web
  • @user1666691 看起来有一个空白的referer,我已经编辑了答案并在重定向规则上方添加了一个空白referer RewriteCond %{HTTP_REFERER} !^$ 的检查。
  • 太棒了。这解决了这个问题,但热链接仍然有效,即如果用户直接转到domain.com/img/000/000/158/test.jpg,他们会在应该发送到 domain.com/img/index.php?querystring=$1 时获取图像
  • @user1666691 在第二个规则集中添加了RewriteCond %{HTTP_REFERER} !^$ 行和[OR],以便引用者是本地的或者根本没有引用者,规则被应用并重写为@987654328 @
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-09
  • 2011-11-17
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
相关资源
最近更新 更多