How to redirect the WordPress attachment url to parent post url

Each time we create some post in the WordPress along with some attachment it will not only create a URL for the post but also for the attachment. For example, a post with an image; both the post and attached image has different URLs and also marked as a separate entity by the Google crawler. This will become a little problem in the Google search because both URLs will show the same post name in the Google search results. The interesting thing is you don’t which one ranked successfully in the search results. So, suppose your WordPress-attachment page has ranked successfully by the Google in searches but wait it is just a page of attachment and when a user clicks to open it what he/she sees is just an attached image… Now you can imagine how it will increase bounce rate and affect the loyalty of readers.

So, here in this post will let you know how to redirect attachment URLs of WordPress post to the original post.

Method # 1: Add redirect code in the function file

It is one of the simple methods to quickly redirect-image-URL of all attachments pages to the original post corresponding to them.

  1. Go to Appearance option given in the left side menu of your WordPress dashboard.
  2. Click on the Editor.                                                                                                        redirect attachment
  3. Select your current theme from the drop-down box given on the right side.
  4. Click on the Funtion.php file from the right side pan to access its codes.               redirect images file
  5. Now copy the below-given code and add it at the end of function.php without altering any other code.
    add_action( 'template_redirect', 'wpsites_attachment_redirect' );
    function wpsites_attachment_redirect(){
    global $post;
    if ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent != 0) ) :
    wp_redirect( get_permalink( $post->post_parent ), 301 );
  6. After adding the code click on the Update file button.
  7. Now you can check any attachment page you will see it automatically redirected to the post’s parent page.

Method # 2: Single attachment redirection

If you think that you don’t want to redirect all image or attachment file pages URLs to the original post and need for any particular post then here are the steps to follow.

  1. Open the post where you attached the image or any other file.
  2. Click on the image and you will see the pencil icon on the top of it to edit. You can also edit the attachment or image from the media gallery.
  3. Now under display settings option, you will get the Link to drop down box select the Attachment page.
  4. This will link that image to the original post where it has been attached.wordpress attachment page redirect
  5. Also, if you want to make this default for you all upcoming attachment files those you are going upload. Then after uploading an image just before inserting that to a post select Attachment page option and this will marked as default for all your upcoming uploads.


Method #3: Attachment Pages-Redirect plugin

If you think the above two givens methods are a bit tricky then you can simply install a plugin that will redirect attachment pages to post pages.

  1. Go to Plugins.
  2. Click on Add new
  3. On the right side plugin, search box type Attachment-Pages Redirect. (direct link to the plugin)
  4. When the plugin appears, click on the Install Now button.attachment pages redirect
  5. Once the Plugin installed click on the Active button to activate the plugin.
  6. After activating the plugin it will automatically start redirecting your attachments pages URLs you don’t need to any further settings and that’s why it is simplest to handle attachment redirections.

How to redirect attachment URLs to the attachment itself

This is something different to redirecting all attachments to main post pages. Previous, the Yoast-SEO redirect attachment-pages was the option present in the Yoast plugin 7.0 but many WordPress websites or blogs uses the same image in multiple pages can create issues and that’s why they have removed the Redirect attachment URLs to a post or page option. Instead of that, they introduced a new feature which redirects attachment URLs to the actual image URL. Means, when the reader click on the attachment page link then the direction the attached image will show instead of a blank page with the single image.  I hope that you have Yoast SEO plugin and let’s see how to use this…

  1. Go to your WordPress Dashboard.
  2. On the left-hand side menu of WordPress click on the SEO.
  3. As you click on the SEO it will expand to reveal further options.
  4. Select the Search Appearance option.                                                                               redirect attachment URLs to the actual image using Yoast SEO
  5. Now from the Search Appearance-Yoast SEO page select the Media apperance Yoast SEO
  6. Scroll down to find the Media & attachment URLs option.
  7. Toggle the button for ‘Redirect-attachment-URLs to the attachment itself? to YESRedirect attachment URLs to the attachment itself
  8. And this will automatically redirect the attachment URLs to the image itself.

In this way, you can disable the WordPress attachment pages to redirect them to the pages where really they are attached. If you have any issues or questions the comment section is all yours!!

You might also like to know about:



3 thoughts on “How to redirect the WordPress attachment url to parent post url”

  1. Thank you rajkumar.İ had 1200 attachment pages.You solve my problem.i try with function.php. Because yoast didnt help for it.

  2. Rajkumar,
    Thank you for this. Saved us from a lot of time. What about exclusions? Can we add exclusions to certain urls like a pdf file that I have set as an attachment?
    Best regards,

    • Hello Leyteris,

      This will not affect the PDF attachments. They will open as they usually were.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.