Redirect directly accessed images from browser to wordpress template file and retrieve post attachment

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

Skills: Apache, PHP, WordPress

See more: wp content wordpress, what ie wordpress, website template to wordpress, png to wordpress, open mind template, imporant, directly, what is a crawler, wordpress bot, statement template, retrieve, ps wordpress, attachment, png jpg wordpress, webserver post, browser detect, fetch post, redirect requests, gif images website, redirect post

About the Employer:
( 0 reviews ) Italy

Project ID: #4262377

3 freelancers are bidding on average $90 for this job


Hi there, i am very interested to complete this project. please see my past reviews lets complete this project

$100 USD in 3 days
(114 Reviews)

expert here check pm thank nitya

$100 USD in 2 days
(140 Reviews)

Our main focus is to deliver the quality work with full support to our clients at very competitive rates. We will be happy to work with you on long term basis. So please consider us for your project. Hoping for the More

$100 USD in 2 days
(28 Reviews)

Hi, I take this opportunity to introduce myself. My name is Jeni and I am the BusinessDevelopment Executive of annaijobs. Understanding: 1. To Develop Redirect directly accessed images from browser to wordpres More

$70 USD in 2 days
(7 Reviews)