I recently noticed that some of the images on my Wordpress website are the pages with the most visits according to Google Analytics. The images could be accessed by clicking them. The result was the pages being opened in a separate tab (no lightbox etc.). I now deactivated this so now one has to right-click and say "open image in new tab" to get the same results as before. Still I'd like to know how this happens and how I can redirect someone who is accessing the direct image path to the page where the image in on?

I noticed YoastSEO is adressing this issue here: https://kb.yoast.com/kb/redirect-image-attachment-urls/ But it does not work for me.

1 Answer 1


This is possible if each image has an owning page and there is a known pattern between them.

Let's say there is an image called: car.jpg and it is owned by page car.html. In this case you can use the .htaccess to do the conversion using referral detection:

RewriteCond %{HTTP_REFERER} !^https://www.example.com/.*$  [NC]
RewriteRule ^(.*)\.jpg$ $1.html                            [R=301,L]

This means that anyone not loading the image from a page on your site (www.example.com) will get a redirect to the corresponding page of the same name. You can add other types of images too, say jpg and png by changing the regular expression to ^(.*)\.(jpg|png)$ or redirect to a more complex URL such as post.php?image=$1

Now if you have images with names that do not relate to any pages such as car.html having an image called nissan_sentra.png then you will not be able to have an easy mapping and you will have to make a redirect table for each case which is not just more complicated but also rather hard to maintain.

  • Thanks a lot. Will this have any negative effects on SEO as it is a permanent redirect? Jan 18, 2018 at 7:22
  • 301 Redirect is good for SEO because it moves along your page rank. The rank may even increase since the link to the post and those to the image will contribute both to the post after the 301 Redirect.
    – Itai
    Jan 18, 2018 at 12:28
  • Thanks for the information. I was looking for pages that did it your way and it looks like this page is a good example for what you described. I will try it on my page and keep you updated. Jan 18, 2018 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.