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
- How to redirect non-logged in users to a specific page?
- When I change slug, will WordPress create redirect from the old URL?
- Why is WordPress redirecting from http to https on a local environment?
- Why am I getting a 301 on a existing post?
- Get all posts (of any post type) an attachment is used in
- Keeps on redirecting on my development copy
- Password change when the user login first time
- Custom login form redirect to external site
- Implementing global redirection (automatic redirect for all pages who had their slug changed)
- Redirect to custom url when registration fails?
- Exclude a path from WordPress using .htaccess redirects (Apache)
- How to redirect url requests to https? [closed]
- Best way to handle redirects
- Redirect to custom URL after registering from a page with registration form
- Stop unwanted WP redirection to new url
- wp-login.php — redirect logged in users to custom URL
- WordPress Customizer: custom redirection after closing
- Hiding WordPress behind IIS redirects
- Easy way to delete 70k posts and attached media?
- Is there a way to get attachment data?
- htaccess 301 redirect http to https doesn’t work
- Site redirecting from http to https [closed]
- Use htaccess to redirect WordPress to static website in a subfolder
- Defining a custom 404 for non-WordPress parts of site
- Redirect all post into some category to url without category
- Redirect from sub-directory to root – New WordPress 5.2.3 Set up
- How to redirect URL with subfolder to the same URL but without subfolder?
- Site loads very slowly (4-5 minute load time)
- Snippet for own (gr)avatars
- 503 Service Unavailable error was encountered
- SSL and www to non-www redirection works only on homepage – WordPress
- Regex with HttAccess
- Regex redirect rule for missing part of the path
- Page template that redirects user based on role
- How can i resolve 301 redirection Issue on my website?
- Media library quick/inline edit
- https – too many redirects
- Automatically attach image to post if they have the same title
- ReDirect subfolder link to another sub-folder and force SSL
- Two WordPress installations: One in root and one in sub directory – no images in subdir installation [closed]
- A WordPress site with more than one domain name / Parked domains
- Redirect user if they are not logged in
- Logout without confirmation and SAME window on mobile
- How to redirect a wrong search on my wordpress website to the error 404 page?
- store URL global before we redirect
- Redirect to page for KPI/marketing purpouses
- Will deleting WP’s code in .htaccess cause problems?
- Get images attached to a specific page
- Migrating to Tumblr [closed]
- Parked domain redirect
- How can I redirect a request to the site root using htaccess, if there’s not a specific cookie set?
- How to fix automatic redirects?
- Redirecting DNS without breaking everything
- get_bloginfo prints IP instead of url
- How to redirect non existent pages/posts in wordpress using htaccess
- How to redirect all page requests to a single “goodbye” homepage? [duplicate]
- Endless Redirects Suddenly Disable Site
- Redirecting home to /wp-login.php?action=register
- How to redirect a child page to its parent page?
- 301 redirect after removing language directories
- wp_login_form() redirect same page
- How to replace the links http to https
- Redirect user after login to prior page through a wp_login hook
- Redirect *.php to path using web.config file
- How can force redirected to login for two or more pages to view or access
- Auto redirect to different web page
- Login redirects to home page and doesn’t log in
- wp_redirect() not redirecting user to supplied URL
- using wp_rewrite but keep the original url in the address bar
- Some links keeps redirecting to another domain
- Cannot get 301 redirection in htaccess to work (either Redirect or Rewrite)
- Redirect every link that starts with
- login redirect problem – db charset issue?
- .htaccess not working after page slug change
- Check if author or uploader id of the attachment(uploaded) image is match?
- can’t handle a redirect
- Wrong Redirection
- Rewrite rule for incoming urls
- Bulk Attach Or Insert Media To Post Without Opening Post Editor
- Detect if request is coming from wordpress conditional statement
- Can I redirect the http request towards an old folder to the homepage using .htaccess file? [closed]
- WordPress Logout Redirect Follow up
- Facebook redirect buton
- How do make WordPress to let alone my static home page?
- how to get my WP site’s IP? [closed]
- Broken images after importing posts and opening in Gutenberg
- When invalid child page entered, WP redirects to next available page instead of giving Page Not Found
- Redirect non-logged in users to a specific page
- Forward and mask implementation not working,
- Get Specific Files (Only Specific Extension Type All Files in Loop ) from Media
- Redirect from subdirectory to WordPress plain permalink
- Top-level staging-site URL redirects to a different site