Did you know that each time you upload a file to the WordPress Media Library, WordPress creates a separate media attachment page for every single file? This page contains nothing except the media content and has its own automatically generated URL. In most cases, this page isn’t very beneficial, which is why you might want to redirect WordPress attachment pages to the original post or page that the file is attached to.
Why you should redirect WordPress attachment pages
Unless you choose the option to Link To the Attachment Page, the actual images in your content shouldn’t reference the attachment page:
But even beyond that issue, there are some other reasons to consider redirecting attachment pages to the parent post.
First, there’s SEO. Let’s say you create a post and add three images to it. WordPress then automatically creates four URLs, three for the images, and one for the original post. This can hurt SEO in more ways than one:
- Google may start bringing more traffic to the attachment pages instead of the original post to which they belong. Like 404 errors, this is something you want to avoid.
- This standalone attachment appears out of context, and a reader may not be able to make sense out of it. Visitors landing on the image or attachment page are likely to close the link and move away, thereby increasing your bounce rate.
- It’s possible that Google may index all the image files and consider it as duplicate content.
Finally, there’s also a niche situation where access to content in pages and posts is restricted by a password. It may happen that someone shares your images on social media. By clicking on the image URLs, an unauthorized visitor may be able to access the media content within these posts or pages despite not knowing the password.
How to redirect WordPress attachment pages
You can help your readers skip these attachment pages by redirecting them to the post or page that they belong to. You can do this using either some free plugins or with your own code snippets.
1. Using Yoast’s media settings
Yoast SEO has built-in options to help you redirect WordPress attachment pages. Once you configure the settings, the plugin takes care of all attachment URL redirects, without any further work on your part.
- Click on the new Search Appearance menu link under SEO in your dashboard sidebar.
- Navigate to the Media tab.
- Find the option to Redirect Attachment URLs to the attachment itself.
- Click on the Yes to turn it on
- Save the changes at the bottom of the page.
That’s it, all your WordPress media attachment pages will be redirected to the parent post or page.
Keep in mind that this redirect option will only work if the media item was uploaded to the original post in the first place. To find out if any media item remains unattached to any post or page, go to your Media Library.
- Make sure you’re in List View.
- Click on the drop-down menu under All Media Items.
- Select Unattached
- Click on Filter.
A list of Unattached Media Items will appear, and if you wish, you can upload each item to the respective post.
2. Other plugin options to redirect attachment pages
Not using Yoast? Not to worry. The other popular SEO plugin, All in One SEO Pack also includes a setting to redirect WordPress attachment pages. In All In One SEO Pack’s General Settings, check the box to Redirect Attachments to Post Parent:
Yet another plugin that’s simple and effective is Attachment Pages Redirect. There are no settings involved with this plugin – simply install and activate. It makes two kinds of redirects. First, it redirects the attachment to the parent post if the post is still active (301 permanent redirect). And in case the parent post is no longer available, it redirects the visitor to your home page (302 temporary redirect).
3. Redirect WordPress attachment pages using code
It’s also possible to redirect WordPress Attachment Pages to the homepage or the original post using a bit of code. You’ll need to add a single line of redirect code to the top of your image.php file (this file is available as part of your theme).
<?php wp_redirect(get_permalink($post->post_parent)) ; ?>
Then save the file and upload it to the root directory of your child theme. Working with the child theme ensures that you can always go back to the previous settings if things don’t go the way you expect.
4. Option to disable attachment pages completely
Instead of merely redirecting attachment pages, it’s also possible to disable them completely. This option does get a bit technical and may not suit many websites (such as portfolio based websites). But if you wish, you can read more about it in this Q&A on Stack Exchange.
Separate media pages may work for photographers, artists, and graphic designers, as they help to create galleries and sliders. But for standard WordPress users, it makes sense to redirect WordPress attachments pages to the posts or pages that they rightly belong to. As a bonus, your site’s SEO may improve.
If you’re using Yoast SEO already, your best option is to just take advantage of Yoast SEO’s built-in setting. Otherwise, the Attachment Pages Redirect plugin provides a nice standalone solution.