Mod_Rewrite条件有助于热链接,但允许本地请求 [英] Mod_Rewrite conditions help for hotlinking but allow local requests

查看:44
本文介绍了Mod_Rewrite条件有助于热链接,但允许本地请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有热链接保护功能,所以我想,但不是对于引荐来源网址为本地。

I had hotlink protection working, so I thought, but not for when the referrer is local.

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]

我需要的是:

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

If the referrer is not from my site on any image request on the site, then redirect to the page with what image the user is requesting.

如果引荐来源网址是我的网站,并且请求的是image_dir中的任何图像,请重定向,否则不要重定向。

If the referrer is from my site and the request is for any image in the image_dir, then redirect, otherwise do not.

任何帮助都将不胜感激!

Any help is greatly appreciated!

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

Here is the full file to help:

## 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] 

除热链接外,其他所有功能都与我想要的完全一样:

Everything works exactly like I want except hotlinking:

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)

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

I still have to allow all requests to:

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

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

Hopefully this helps clarify what is going on. Apologies for not doing this in the beginning.

推荐答案

尝试改用以下规则:

# 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]

也就是说,我假设您的问题中有这个神秘的地方:

That is, I'm assuming this cryptic bit in your question:


如果引荐来源网址是我的网站,并且请求的是image_dir中的任何图像,请重定向,否则不要重定向。

If the referrer is from my site and the request is for any image in the image_dir, then redirect, otherwise do not.

表示:

如果引荐来源来自我的网站,且请求针对image_dir中的任何图像,将请求发送到some_file.php处理程序,否则不做任何事情。

"If the referer is from my site and the request is for any image in the image_dir, send the request to the some_file.php handler, otherwise don't do anything."

这篇关于Mod_Rewrite条件有助于热链接,但允许本地请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆