如何在允许嵌入的同时使 htaccess 重定向直接图像访问

发布于 2024-10-05 14:49:37 字数 543 浏览 0 评论 0原文

场景如下所示,假设我有一个包含一些图像的网站。我希望允许人们将它们嵌入到他们的网站上,但我也想在任何人尝试直接访问图像时进行重定向。

一种选择是

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?imgzzz.com/.*$ [NC]
RewriteRule i/image_(\d+)\.(jpg|gif) - [F]

拒绝来自网站以外任何地方的任何访问,但不允许嵌入图像。

第二个选项是省略第二个 RewriteCond ,仅保留

RewriteCond %{HTTP_REFERER} !^$
RewriteRule i/image_(\d+)\.(jpg|gif) - [F]

允许嵌入的内容,但如果用户从书签以外的任何位置访问图像,仍然可以访问该图像。

有什么方法可以使嵌入可用,同时在直接访问图像时进行重定向?

The scenario looks like this, say that I have a website that contains some images. I want to allow people to embed them on their site, but I also want to redirect when anybody tries to access the image directly.

One option is this

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?imgzzz.com/.*$ [NC]
RewriteRule i/image_(\d+)\.(jpg|gif) - [F]

which would deny any access from anywhere else than the site, but that doesn't allow embedding the image.

The second option is to ommit the second RewriteCond leaving only

RewriteCond %{HTTP_REFERER} !^$
RewriteRule i/image_(\d+)\.(jpg|gif) - [F]

Which allows embedding, but the user can still access the image if he's comming from anywhere else than a bookmark.

Is there any way to make embedding available and at the same time doing redirect on direct access to the image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹沉浮 2024-10-12 14:49:37

这是一个解决方案

RewriteCond %{HTTP_REFERER} !^http://(www\.)?imgzzz.com/.*$ [NC]
RewriteRule i/image_(\d+)\.(jpg|jpeg|gif) pic/$1 [L]

Here is a solution

RewriteCond %{HTTP_REFERER} !^http://(www\.)?imgzzz.com/.*$ [NC]
RewriteRule i/image_(\d+)\.(jpg|jpeg|gif) pic/$1 [L]
一向肩并 2024-10-12 14:49:37

不,这只是对 URI 的请求,无论它是页面的一部分还是直接访问,服务器都看不出任何区别。

我们-嗯,好吧,我想理论上你可以用 php 和 php 做一些疯狂的黑客攻击。 javascript,但我不认为它值得这么麻烦,而且它也不可靠。话又说回来,这只是一个请求,你无法辨别它是如何产生的。它还会扰乱某些人的浏览方式,即喜欢在新窗口中打开图像的人等。我想我知道您想要实现什么(而不是图像,您希望提供一个包含一些数据和图像的页面,以使内容和您的网站更具可见性),并且我倾向于同意这是一个有效的想法,但图像搜索引擎已经做了这些事情。

如果我必须选择,我会选择第二个选项,它足够宽松,允许正常浏览,并且不允许图像的外部链接。

No. It's just a request to a URI, whether it's part of a page or being accessed directly, the server can see absolutely no difference.

We-e-ell, okay, I guess you could theoretically, perhaps, do some crazy hackery with php & javascript, but I can't see it being worth the hassle, and it wouldn't be reliable either. Then again, it's just a request, and you have no way of discerning how it originated. It'd also disrupt the way how some people browse, i.e. people who like to open images in new windows &c. I guess I know what you'd like to achieve (instead of an image, you'd like to serve a page with some data along with the image to give the content and your site more visibility), and I tend to agree it's a valid idea, but image search engines already do these things.

If I had to choose, I'd go with second option, it's permissive enough to allow normal browsing, and would disallow external links to the images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文