How to make htaccess redirect on direct image access while allowing embedding

我与影子孤独终老i 提交于 2020-01-02 15:17:12

问题


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?


回答1:


Here is a solution

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



回答2:


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.



来源:https://stackoverflow.com/questions/4317168/how-to-make-htaccess-redirect-on-direct-image-access-while-allowing-embedding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!