How to redirect WordPress attachment pages to original post or page?
Attachments to posts are stored in the database as posts, this means they’re accessible under their own URLs. Enabling the redirect image feature will redirect these URLs to the actual image.
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.
How to redirect WordPress attachment pages?
You can redirect WordPress attachment pages using either some free plugins or with your own code snippets.
1. Using advanced settings in the Yoast SEO plugin
Yoast SEO includes options under the Advanced Settings Page that help you to 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.
The Advanced Settings Page is not immediately visible on the Dashboard. To access the page, you’ll first have to enable it. To do that:
- Click on the SEO option
- Go to the Features tab
- Enable the Advanced settings pages
- Click on the new Advanced menu link under SEO in your dashboard sidebar.
- Navigate to the Permalinks tab.
- Find the option to Redirect Attachment URLs to parent post URL.
- Click on the Redirect button 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
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
<?php wp_redirect(get_permalink($post->post_parent)) ; ?>
Then save the file and upload it to the root directory of your child theme.
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.
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.