Hi pressword1
We redirected all the linked-attachment urls to the parent post/pages url’s.
All the unlinked media attachment’s are redirecting to the homepage.
We are not using the function in de Yoast SEO plugin because is doesn’t redirect unlinked media. We wrote are own code for it in the functions.php. I wrote a (dutch) blog about the attachment url’s. If you have any questions about it let me know.
add_action( 'template_redirect', 'attachment_redirect', 1 );
/**
* Redirect any attachment page to their parent with 301 redirection
*/
function attachment_redirect() {
global $post;
if ( is_attachment() AND isset( $post->post_parent) AND is_numeric( $post->post_parent ) ) {
if( $post->post_parent !== 0 ):
wp_redirect( get_permalink( $post->post_parent ), 301 );
else:
wp_redirect( site_url(), 301 );
endif;
exit();
} elseif( isset($_GET['attachment_id']) ) {
wp_redirect( site_url(), 301 );
}
}