When a user attempts to load an image directly into the browser (for example coming from Google Images), the browser normally opens just the bare image, without any reference to the WordPress website where that image was originally uploaded to.
I'm looking for a reusable template to address the following situation (an example):
1) User is browsing Google Images and finds an image (or tries to access an image directly, by any means).
2) Google Images offers the user to open the website where the image was found or just open the original image.
3) The user chooses the latter, therefore the ball is now on the webserver where the image is hosted.
4) htaccess detects a human operated browser trying to access directly to an image in /uploads/ folder.
5) htaccess redirects the user to [url removed, login to view], however passing the original requeste URL in the post value.
6) [url removed, login to view] which normally processes attachment in this case takes the URL originally requested and uses url_to_postid() function to retrieve the original post where that image was attached to. If no post attachment is found, redirects to homepage.
7) However, if the request comes from a bot (ie Google Images crawler) the images are not redirected and can be accessed directly.
So, what I'm looking for is
a) some .htaccess snippet to redirect requests from human browsers who try to access images drectly - jpg, jpeg, png, gif... - in the Uploads folder directly (important: mind that the uploads might be in a different folder than wp-content) to a wordpress template file (I was thinking '[url removed, login to view]' and use some conditional php statement - if... else - in case there's not a wp query running but a redirect from htaccess).
b) a snippet for the template file which would get the post value from the URL to fetch the original URL requested (to the image file) and use that to retrieve the original post where the image could have been attached to. Finally, redirects the user to this post.
My biggest difficulty in this was to figure out a reusable htaccess snippet to detect human browsers trying to directly accessing image files uploaded in WordPress and redirect successfully to a wordpress template file while carrying the original URL requested in the redirect post value.
ps - naturally this shouldn't work only for google images, the imporant thing would be to tell the difference between a bot and a human operated browser