5 Ways To Redirect Attachment Pages To The Parent Post URL

I was looking in GWMT recently and noticed 400 more URL’s than posts published have been indexed. I guess its because of image attachment pages which is why i’ve written this post.

GWMT Index Status

To redirect attachment pages to the parent post URL, there are at least 5 different solutions to choose from:

  1. Write a custom function with conditional tag using both the wp_redirect function and template_redirect function.
  2. Add 1 line of redirect code directly to your themes attachment or image.php file.
  3. Use the settings in the WordPress SEO by Yoast plugin if installed.
  4. Install the Attachment Page Redirect plugin or similar.
  5. Genesis Users: Create a new attachment.php or image.php file which includes the redirect to post parent URL code and upload it to your child themes root directory.

1. Custom Function With Redirect

Simply copy this PHP code from the view raw link in the Gist and paste it at the end of your child themes functions.php file.

2. Add Redirect to image.php

  1. Create a new file using a code editor.
  2. Copy the code from the View raw link in the Gist labelled image.php
  3. Paste the code into the file and upload it to your child themes root directory.

Tested on the Twenty Fourteen default theme for WordPress.

3. WordPress SEO Permalink Settings

permalink settings - redirect urls

4. Attachment Page Redirect plugin

Attachment Pages Redirect

5. Genesis Attachment File With Redirect

  1. Create a new file named attachment.php using a code editor like Notepad++
  2. Copy the PHP from the view raw link in the Gist.
  3. Paste the code into the file and upload it to the root directory of your child theme

If you have found another way to redirect attachment pages to the parent post, please add the solution in the comments.

I’ll update this post if it makes any difference and let you know what happened.

Related Tutorials


Comments

7 responses to “5 Ways To Redirect Attachment Pages To The Parent Post URL”

  1. Thanks for the nice code snippet Brad. Unfortunately, Yoast SEO now redirects Attachment URLs to direct media file instead to the attached page or post, so used the first method and it works great!

    Kind regards,
    Shyam

  2. Most of my attachment pages are indexed in Google. Is all attachment pages will removed once i follow above method?

  3. What do I do to remove the index Attachment URLs
    illustrating images: https://i.imgur.com/16u6E3h.png
    This is the link I used in the image above: https://agiare.vn/pin-sac-du-phong.html
    Thanks!

    1. Coding support is only provided to registered users https://wpsites.net/register/

  4. I’ve been using something similar to method 1 but have recently found ‘unattached’ attachments aren’t redirected. Any suggestions on how to redirect ‘unattached’ attachment images to, let’s say, the homepage?

  5. Asif Icbal Avatar
    Asif Icbal

    Does this method redirect attachment files too?

    1. Brad Dalton Avatar
      Brad Dalton

      Depends on which method you use.

      You can redirect all attachments, image attachments only or anything you like depending on which method you use.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.