How to make htaccess redirect on direct image access while allowing embedding
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?
Here is a solution
RewriteCond %{HTTP_REFERER} !^http://(www\.)?imgzzz.com/.*$ [NC]
RewriteRule i/image_(\d+)\.(jpg|jpeg|gif) pic/$1 [L]
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.
精彩评论