热链接重定向
我的网站上有以下 URL:www.example.com/yyy.gif。 它实际上不是图像的直接链接,而是包含该图像的 HTML 页面(该图像的直接 url 是 www.example.com/files/yyy.gif)。我想保持这种状态。
问题
当链接 (www.example.com/yyy.gif) 发布到某个地方(论坛、各个网站的评论)时,他们的脚本通常会认为它是指向图像的直接链接并尝试将其显示为图像 (),这会导致其网站上的图像损坏。
问题
有没有办法检测这些情况并自动将它们重新路由到直接图像 URL?请记住,访问者应该能够打开原始 URL,而不会被重定向。
I have this URL: www.example.com/yyy.gif at my site.
It is actually NOT a direct link to the image but a HTML page containing the image (direct url to said image is www.example.com/files/yyy.gif). I want to keep it that way.
THE PROBLEM
When the link (www.example.com/yyy.gif) is posted somewhere (forums, comments at various websites) it is quite common that their script assumes it is a direct link to an image and tries to display it as an image (<image src="www.example.com/yyy.gif">
) which leads to broken image on their site.
THE QUESTION
Is there a way to detect these cases and automatically reroute them to the direct image URL? Keep in mind that visitors should be able to open the original URL without being redirected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查 $_SERVER['HTTP_REFERER'] 变量以查看该网址是否源自您的网站。
如果没有,请输出图像的一些标题,然后输出图像本身,而不是通常执行的操作。
You could check the $_SERVER['HTTP_REFERER'] variable to see if the url originates from your site.
If it doesn't, output some headers for the image and then the image itself instead of what you would normally do.
我想我明白了!
因此,我检查了当通过单击链接访问相同的 URL 以及使用
标记加载该 URL 时会生成哪些请求标头。
唯一不同的参数是“接受”。当
访问 URL 时,它是
*/*
(Chrome、IE、Safari)或image/png,image/*;q= 0.8,*/*;q=0.5(FF)。
但是当通过点击链接(或者直接打开URL)访问时,它是这样的
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
因此,在输出任何内容之前,我会在脚本中检查“text/html”。如果存在,我输出 html 版本;否则直接去寻找图像。
它似乎完全按照我想要的方式工作(耶!)。
有什么我应该注意的陷阱吗?
I think I got it!
So I checked what request headers are generated when the same URL is accessed by clinking on a link and when it is loaded using the
<img>
tag.The only parameter that differs is "Accept". When the URL was accessed by
<img>
it was*/*
(Chrome, IE, Safari) orimage/png,image/*;q=0.8,*/*;q=0.5
(FF).But when accessed by clicking on a link (or just opening the URL directly), it is something like this
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
So I'm checking for 'text/html' in my script before outputting any content. If it is present, I output the html version; otherwise go for the image directly.
And it seems to be working exactly as I wanted (yay!).
Could there be any pitfalls that I should be aware of?