Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Redirect direct image urls to wp template file and retrieve post WordPress


When a user attempts to load an image directly into the browser (for example from Google Images), the browser normally opens just the bare image, without any reference to the wordpress installation where that image was originally uploaded to.

I'm trying to make a reusable template to address the following situation:

1) User is browsing Google Images and finds an image.
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 single-attachment.php, however passing the original requeste URL in the post value.
6) single-attachment.php 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 (mind that the uploads might be in a different folder than wp-content) to a wordpress template file (I was thinking 'single-attachment.php' and use some conditional php statement 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

Answers (1)


Fahad Murtaza answers:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteCond %{REQUEST_URI} ^/uploads/([^/]+)/? [NC]
RewriteRule . page.php?url=uploads/%1 [L]

So, with that in .htaccess, edit the page.php and based on the value of image name, redirect to the post holding the image. That assumes that all images on your website use the /uploads/ folder in their URL, i.e all images were uploaded using wordpress admin panel.

Gaslight comments:

nice try, that is pretty much the copy from an answer to a question I did post on stackexchange few days ago :)

and it ain't working fine

Gaslight comments: