How to Redirect Attachment Image File to Parent Post in WordPress many people searching for this including me but we can’t prevent or disable Google to index these files just move to original post or page.I noticed multiple “image pages” indexed on Google. Whenever you add an image to your post it automatically attaches a link to the image that goes to a blank post (WordPress stores attached image data as posts in the db) with nothing but that image.
The issue is that these pages are virtually pointless as they have no content. You are better off linking to your actual image itself if you want people to be able to click and see a large version.
So after a bit of browsing a found a really good solution to help you “disable” these image pages and bring the page rank back to your main post. Technically it won’t disable the image attachment pages, but it redirects the page back to the primary post.
Image Attachment Pages Using A WP Redirect:
- Create a new file called “image.php” or delete everything on your current image.php file (if present)
- Insert the code below in your image.php file using a text-editor and upload to your theme’s directory.Or you Download file here
<?php wp_redirect(get_permalink($post->post_parent)); ?>
Paste it in Root> wp content> Template> <your theme> Paste here.
Now when you refresh your image attachment page it should redirect to the original post Enjoy!