Attachments not redirecting to parent pages
With one of the latest Yoast SEO versions (7.x), the attachment redirection function has changed, breaking some of the pages and not functioning as described in the Yoast Knowledge Base.
See this info: https://kb.yoast.com/kb/redirect-image-attachment-urls/
The options says “Redirect attachment URLs to the attachment itself?” and checking this box does exactly that.
Meanwhile, prior to Yoast 7.x (not sure which one exactly), the option basically was “Redirect attachment URLs to PARENT POSTS WHICH THEY ARE ATTACHED TO?”.
On the info page linked above, the description says: “Redirect will automatically redirect the attachment URLs to the parent post.” and there’s info on how to attach or detach attachment from posts.
This change in behaviour was therefore rather not intentional and is a bug. Further, websites which use image galleries to link to pages or posts which the attachments are attached to are now broken, as link point to images rather than posts.
Please correct this function so that it still redirects attachments to the parent posts which they are attached to.
In an ideal situation, users should have the following options:
- Redirect attachment URLs to parent posts which they are attached to (on by default AND TURNED ON IF THE CURRENT AND PREVIOUS REDIRECTION SETTING WAS TURNED ON, for backwards compatibility) – use the function which was used in Yoast 6.x
- Redirect attachment URLs to attachments themselves – use the function which was used in Yoast 7.x
- Do not redirect attachment URLs
Interlinked with forum post: https://wordpress.org/support/topic/bug-attachments-not-redirecting-to-parent-pages/#post-10101803
Best, Chris
Our site is one of those affected by this latest Yoast upgrade, all my gallery page images not redirecting to post page of which there were 400 posts, have spent a week trying to find the problem with not a great deal of help from Yoast except offering to help and waiting for more than 24 hours for them to get back to you with not much help. The latest upgrade of Yoast if you can call it an upgrade, corrupted all of my meta descriptions on 400 pages, my sitemap redirected to the home page and Google Webmaster couldn't find them. As a paid member of Yoast this has been a very disappointing experience, especially with support which has come from three different people and almost robotic in reply, almost a week of downtime for my website.
As a temporary solution, turn off the "Redirect attachment URLs to the attachment itself?" option and use the following code in your functions.php file:
add_action( 'template_redirect', 'sukcesstrony_attachment_url_redirect' );
function sukcesstrony_attachment_url_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 );
wp_reset_postdata();
exit();
}
}
Where do you recommend to put the code? At the bottom of the function.php file? Sorry, I don't know a lot about coding.
Also, on the wordpress forums another user said if the attachment media is posted in more than one blog post there is a redirect problem? Would that still exist or would it only redirect to the original or parent post, i.e. other blog posts could have the image, but they wouldn't be child of the parent or original post.
Thank you SukcesStrony! I have applied this filter too in my functions.php file and now the attachment URLs are redirecting to parent posts but for my own info kindly tell me that yoast recommends we should redirect attachment URLs to the attachment itself but when I do this, my attachment sitemap gives 404, not found error and they say media is set to no index. So, how it is possible that we set it to yes and even then our images are indexed?
@Gf177 Doesn't matter that much where you put this code. Top of the functions.php file will be fine. If you want to keep things tidy, use the "Code Snippets" plugin to paste such code. An attachment can be "attached" to only 1 page or post (selected in the Media menu). Using the same image file in 2 different posts or pages doesn't affect redirecting. What can be troublesome is when an attachement is not attached to any post or page - then the redirect obviously won't work. But that's not a bug and can be fixed by attaching the attachment in the Media section.
@Sandzee Yoast SEO probably detects that the attachment page URLs are redirected and so there's no point in making a sitemaps file for them, as they are redirected anyway. Be sure to understand that the attachment sitemaps are sitemaps for dynamically generated URLs like: domain.com/ATTACHMENT-FILE-NAME/ - this displays the attachment inside the theme (with the menu, sidebars, footers) and this is redirected and therefore not indexable domain.com/ATTACHMENT-FILE-NAME.EXTENSION - this is the exact URL of the file and is NOT redirected, and is indexable So, with the code above you're not redirecting the media file itself, just an URL which WP generates automatically for each media file.
ok thank you.
Thank you to Sukces Strony,
After more than three weeks hoping that the Yoast techies would help me solve the problem of images not being redirected to the post page and many emails to them regarding this issue, the code you have provided is perfect, images redirect to posts as they did before the 7.0 Yoast upgrade. Just wondering though, as you say it is a temporary solution, when Yoast brings out a new upgrade will this code still work?
Thank you so much,
On Fri, Mar 23, 2018 at 9:27 PM, SukcesStrony [email protected] wrote:
As a temporary solution, turn off the "Redirect attachment URLs to the attachment itself?" option and use the following code in your functions.php file:
add_action( 'template_redirect', 'sukcesstrony_attachment_url_redirect' );
function sukcesstrony_attachment_url_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 ); wp_reset_postdata(); exit(); }
}
}```
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Yoast/wordpress-seo/issues/9316#issuecomment-375629581, or mute the thread https://github.com/notifications/unsubscribe-auth/AKCdqi66MFbDxnMpPLwXX-k6odARj7Ueks5thNwzgaJpZM4S27eG .
@maureen5 One of the redirections (the one in the code above or the one introduced by Yoast) will take priority. Likely it will be the function run by Yoast as the one above has a default priority of 10.
Please inform the customer of conversation # 342030 when this conversation has been closed.
Please inform the customer of conversation # 344309 when this conversation has been closed.
Attachment Pages get redirected to the Image File itself:
Set Redirect

See Attachment Page

Click on image and instead of being redirected from the attachment page back to the main url it goes to the image file

This is as expected from v7.0: https://yoast.com/yoast-seo-7-0/
Please inform the customer of conversation # 357610 when this conversation has been closed.
On one of my sites, this bug somehow reindexed all the attachment pages, shooting tons of low quality content out onto Google, bringing down both search ranks and traffic overall by the time I figured out what the hell happened and manually fixed the problem. Please inform me when this is addressed.
I do have simply blog posts with featured image. I checked all settings and updated to the latest yoast version. The attachment re-direct does not work. All featured images are indexed with attachment pages. This is not good and a major bug.
Please help. This became a huge problem and simply irritates all users who trust the yoast brand over years. Thank you very much for all your help and fixing that problem! I really do appreciate it!
Hey sort of half-relevant to this:
I have a few images that we changed the file name for OR the image has been deleted. Now those old image urls are returning 404s.
I've tried to add the old image url (with the .jpg extension) and redirect them to either the homepage or the newly named version of the image url using the latest version of Yoast's premium redirect manager but it doesn't seem to be working...
Any help would be appreciated!
@SukcesStrony I looked again at the solution and actually there are two issues here. Just redirecting pages may not be the best solution.
The big issue is that the pages are already indexed - you need to remove them ASAP. How to remove these pages from the Google index:
You'll need to mark them as NOINDEX. Otherwise they'll stay in the google index for a VERY long time. To do that - in the yoast plugin:
Redirect attachment URLs to the attachment itself? -> select NO Show Media in search results? -> select No [super IMPORTANT}
This will add a noindex to all media pages and Google will remove them from the index. If you'll redirect these pages Google will miss the noindex so you must NOT redirect them.
But here's the catch once you marked "Show Media in search results?" to be No. They will not be in the sitemap so potentially Google wouldn't bother touching them and will not see the no index.
You need to submit a sitemap with these pages and change the mod date.
See this on how to deindex page quickly: https://www.seoinc.com/seo-blog/fastest-way-to-deindex-pages/
See this related article: https://www.bloggingglobal.com/attachments-pages-redirection-not-solving-problem-search-results/
After you've verified that all media pages have been removed you can redirect them if you want. But the noindex will prevent them from appearing the Google index.
I need to be quite clear and say that I completely lost trust with the YOAST SEO plugin. My site was in great shape but that change resulted in 4,000 thin content pages submitted to Google index. I now need to clean up the mess that they did and recover from the Google penalty that it caused. No QA in Yoast? This is a massive loss in my side
So how in heaven can we fix that?
@jack2345A thanks, I've fixed it.
I'm sorry some people got the wrong setting here during the upgrade. I can understand people are annoyed, as we also really didn't want this to happen.
There's not much for us to fix though: just set the first setting to "Yes" on the Media tab and attachment URLs no longer exist. That should take care of the problem in no-time. We will add a notice in an upcoming release, asking people to re-check this setting specifically when it's set to "No".
@jdevalk - the advice that you give will put websites in more trouble,
You need to tell Google to NOINDEX these pages - they don’t need to be in the Google index. They need to get removed as fast as possible.
When setting to "Yes" the first setting Google will still keep these pages. There is really no point to redirect Google to the images as it serves no purpose and just loading the Google index with thin content as the images are already on the parent page. Google is smart enough to extract the images.
I am still at a loss why you made this change - this absolutely destroyed Google traffic. There was nothing wrong with the previous way that this was handled. It worked great for many years.
You obviously know that recovering from Google penalty is not a matter of "no-time". It may take months. Also many of these pages are orphaned pages so you really need to tell Google that something has changed by changing moddate in the sitemap.
Is anyone in the Yoast team really know what they are doing - this is such a basic error in your side. There is really no need in having these media pages in the Google Index. You are destroying the livelihood of hard working website owners.