• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2785

Original Genesis & WooCommerce Tutorials & 6000+ Guaranteed Code

Snippets

  • Try Premium
  • Log in

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

  • Genesis Attachment Page for Images

Reader Interactions

Comments

  1. Shyam Chathuranga says

    May 6, 2018 at 7:40 pm

    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

    Log in to Reply
  2. Sasi Kumar says

    April 4, 2018 at 11:17 am

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

    Log in to Reply
  3. Son Le says

    March 10, 2018 at 3:25 pm

    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!

    Log in to Reply
    • Brad Dalton says

      March 11, 2018 at 11:54 pm

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

      Log in to Reply
  4. Henry says

    August 29, 2014 at 12:37 pm

    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?

    Log in to Reply
  5. Asif Icbal says

    July 4, 2014 at 11:06 am

    Does this method redirect attachment files too?

    Log in to Reply
    • Brad Dalton says

      July 4, 2014 at 11:35 am

      Depends on which method you use.

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

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags
 

Loading Comments...
 

You must be logged in to post a comment.