Yoast SEO (and probably other SEO plugins as well) handles that issue by redirecting to the image itself.
I’m not sure how they do it, but you can either go the quick route and install the plugin, or you try to look trough the code to decipher it for your own code.
Otherwise, this is maybe better handled by adding something to robots.txt?
Related Posts:
- Redirect Attachment Page to Attachment
- Media/attachment urls don’t redirect with fake URL structure
- Create a permalink to a pdf?
- how to redirect 404 to attachment.php?
- 301 Redirect Link to Media
- Redirect attachment URL
- redirect attachment page to category page
- Stopping media file redirects
- PHP header(Location: …): Force URL change in address bar
- What does HTTP/1.1 302 mean exactly?
- php refresh current page?
- How to force a 404 on WordPress
- How to Fix HTTP Error When Uploading Images?
- Disable front end to use as CMS only?
- Redirect Restricted Page to 404
- How to manage attachment relationships for specific posts in WP 3.5+
- How to redirect non-logged in users to a specific page?
- How to prevent automatic redirection of 404 errors and “incorrect” URLs?
- How do I skip wordpress’s 404 handling and redirect all 404 errors for static files to 404.html?
- Disable WordPress URL auto complete
- Host the wp-admin on another domain?
- Redirect user to original url after login?
- When I change slug, will WordPress create redirect from the old URL?
- WordPress localhost site redirect to live site
- WordPress redirecting connections on port 8080 to 80
- WordPress Redirect All HTTP requests to HTTPS via .htaccess
- How to trigger a refresh in the media modal
- wp_redirect() function is not working
- How to use taxonomies on attachments with the new Media Library?
- Redirect entire website to a single page
- My wordpress multisite homepage redirects to signup page
- Logout redirect to home page
- Disable ONLY URL auto complete, not the whole canonical URL system
- Redirect Localhost wordpress site to dashboard
- Redirect page URL to home URL without using a plugin
- Why is WordPress redirecting from http to https on a local environment?
- Login redirect to previous page
- How to prevent redirect to ‘About’ after core upgrade?
- 301 redirect instead of 404 when URL is a prefix of a post or page name?
- Why is wp_redirect() preferable to a standard PHP header redirect?
- How do I programmatically generate a 404?
- How can I bulk delete media and attachments using WP-CLI?
- Redirect to referring page after logging in
- User redirect to destination URL after login
- Prevent access to single post types
- check first time login only
- Stop WordPress from reserving slugs for media items?
- How to change the default logout link on WordPress Admin
- Stop WordPress from “guessing” redirects for nonexistent URLs
- Changing attachment urls?
- Can I use the wp media uploader for my own plugin?
- Open media frame and select an attachment
- WordPress install enters an infinite loop
- How to redirect a sucessful registration to a page template?
- How to configure nginx to redirect requests to the uploads directory to the production server?
- WordPress HTTPS redirect loop
- Why am I getting a 301 on a existing post?
- Redirect to dashboard after login
- Make a video post redirect to next post after completion
- media_handle_upload for local files?
- Expanding new Media Uploader in WordPress 3.5
- Put the media uploader in a metabox
- Uploading Multiple Attachments From Front-End With A Description
- get_attached_media() returns empty array if media file already used by another post
- Old blog/ and blog/feed/ URLs not working after moving blog to top-level via .htaccess
- How do I detach images from posts?
- Non-WordPress page in subdirectory under WordPress page
- Force HTTPS using .htaccess – stuck in redirect loop
- How to redirect after login, the working way?
- Get all posts (of any post type) an attachment is used in
- How to get all files inserted (but not attached) to a post
- Keeps on redirecting on my development copy
- Password change when the user login first time
- Custom function to rearrange attachments when creating posts – Almost there
- Custom login form redirect to external site
- Redirecting WordPress /.htaccess / HSTS / SSL
- 301 redirect not working through .htaccess
- Password protect some uploaded files, so only logged-in users can view them
- How to single click to download image in single post
- handling login/logout redirects
- Clarification on auth_redirect()
- Replacing all attachment links in post with media file link
- Unattaching unlinked images
- Site redirects to wrong url when saving settings
- How to redirect all requests to wp-login.php?
- Implementing global redirection (automatic redirect for all pages who had their slug changed)
- Correct way to hide pseudo pages from being shown?
- Redirect blog page to latest post?
- Redirect to custom url when registration fails?
- Why does WordPress automatically redirect URLs with the parameter “name=” to a different page?
- prevent/block direct access to a thank you page
- Redirect After Delete User in Backend
- Exclude a path from WordPress using .htaccess redirects (Apache)
- Disable slow media queries?
- Restrict Access to wp-login.php
- How to setup 301 redirects for multiple query string URLs?
- How to redirect url requests to https? [closed]
- Delete attachments from Front end
- Using `auth_redirect` : keeps asking me to login even when I’m logged in
- Htaccess redirect from ‘/%postname%.html’ to ‘/%postname%’