Using Yoast SEO plugin to redirect attachment pages to the parent post or page

Yoast offers a handy feature which allows you to redirect any attachment page to the parent post or page.

You need to have the Yoast SEO plugin installed for this solution. Download Yoast SEO plugin here, you can also find this plugin in WordPress’ plugin repository, so searching for it through Plugins > Add New and installing it directly will work as well.

Step 1

Login to your WordPress site, once you have gained accessed to the backend dashboard, navigate to Yoast SEO > Advanced.

Note: By default “Advanced” settings are not enabled. To enable “Advanced” settings:

  1. Navigate to Yoast SEO > Dashboard
  2. Select the “Features” tab.
  3. Toggle the “Advanced settings pages” switch to “Enabled“.

How To Enable or Disable Advanced Settings

Step 2

Select the “Permalinks” tab.

Step 3

Find “Redirect attachment URLs to parent post URL.” under the first heading “Change URLs“.

Select “Redirect” and confirm these changes by clicking “Save Changes”.

If attachment pages have parent pages, they will now be redirected to them. If they don’t have parent pages this won’t work and you’ll have to assign them to a relevant page, by “attaching” them to a page through the Media Library, as demonstrated in this Yoast post, or exploring alternative methods.

Not all attachment pages are redirecting. Why?

This will only apply to attachment pages which have parent pages.

If an attachment is uploaded to the WordPress media library, using the “Add Media” tool while editing a post or page, it’ll be attached to this post or page.

But if an attachment is uploaded to media library directly (Media > Library) it will not be attached to any post or page and will remain unattached.

An unattached media attachment will have no parent or post page it could redirect to. There are measures you can explore to address this, but this goes beyond the scope of what we can do using Yoast.

However, Yoast does enable you to specify a noindex rule for unattached media pages:

  1. Navigate to Yoast SEO > Titles and Meta
  2. Select the “Post Types” tab.
  3. Under the “Media (attachment)” sub-heading, toggle the “Meta Robots” switch to “noindex“.

How do I end up with these attachment pages?

For every image that is uploaded, using the media library of WordPress, an attachment page is created.

You can consider this a page like any other page, post or custom post-type you typically create in WordPress, which means you would be able to view this attachment on a front-end through a page; the attachment page.

This is different from viewing the attachment through your browser using the file url – which is the direct path to attachment on your site’s server, and does not render the attachment in a page that looks like any other generic post page.

Attachment page for image:

https://webseo.co.za/wordpress-redirect-attachment-urls-using-yoast/wordpress-redirect-attachment-urls-using-yoast-2

Direct server file path for image:

https://webseo.co.za/wp-content/uploads/2017/10/Wordpress-Redirect-Attachment-Urls-Using-Yoast.jpg

Since these are stand-alone pages, they could be indexed by search bots if they are included in the sitemap or linked to internally, or externally, from a post. Which means these urls could be found using any search engine that may have indexed them.

Alternative methods to redirect attachment pages

Other than the method described above, using Yoast SEO plugin, there are others worth exploring.

Brad Dalton, of WP Sites, runs through 5 alternative methods to redirecting attachment pages in his post 5 Ways To Redirect Attachment Pages To The Parent Post URL.

In addition to some of the alternative methods explored in Brad’s post, the following code snippet can be added to the first line of the active theme’s image.php:

It provides an else condition should there be no parent page the media attachment can be redirected to, in the code snippet above the 301 redirect points to the home page, but this can be changed to any page.

Note: for the sake of best practice always make updates and edits to theme core files through a Child Theme, to ensure that custom edits and updates are not lost, or overwritten, when and if the theme is ever updated.