Redirect All WordPress Image Attachment Pages to Home Page

WordPress generates¬†an individual page for every image and attachment added through the media library — which can be a nuisance if indexed by Google for example. This is how to easily redirect all of these attachment pages permanently with 301¬†to your home page without using a WordPress plugin.

Attachment Pages 301 Redirect

The ‘Attachment Pages Redirect‘ plugin and most searches suggest to redirect the pages to the parent post — but this method can have drawbacks; not suitable for everyone including my self.

To simply avoid having these pages indexed by Google as well as safely de-index them afterwards, we can just point the 301 redirection directly to home page — a straightforward and much simpler solution; without the use of plugins of course.

The Fix

You will want to be using a WordPress child-theme for this.

Create the file “images.php” in the root of your theme folder. If it already exists, you can just over-write the file.

Then add in the following and replace the URL with your own.

<?php
wp_redirect( ‘https://wheresmynotes.com’, 301 );
exit;
?>

And you are done. Questions/comments welcome!

Twitter Button Facebook Button Google Button LinkedIn Button

Leave a Reply

Your email address will not be published. Required fields are marked *