The .htaccess is parsed before WordPress is loaded, so you don’t get access to WordPress data.
You can make the redirect in WordPress, in a plugin: Hook into 404_template and inspect $_SERVER['REQUEST_URI'] with parse_url(). You should be able to find the matching post for the image then.
You did not write how the permalinks for these posts are built, so we cannot offer a more specific solution.
Related Posts:
- Server (WordPress) redirects files that are not supposed to (using htaccess)
- Alias ‘wp-content’ directory to something shorter (framework?)
- add_rewrite_rule fro html to another url not working
- Wildcard forward all posts and pages with few exceptions
- Clash of the rewrites
- Will references to ugly links automatically redirect to their pretty url permalink?
- Hijacking the URL for filtering
- WP Login forms action URLs displayed as pretty URLs in browser (ex with Restore Password)
- custom htaccess rewrite rule for page
- Dynamic URL, not a physical page within the database
- Redirecting all old links from previous EE site to new WP site in one go?
- Prevent WordPress from automatically correcting URLs
- Display content according to current URL
- Rewrite WordPress Custom URL
- Custom rewrite rule
- WordPress .htaccess – route other URLs to another app
- Is there a ‘rake routes’ for wordpress?
- Changing RSS feed URL structure
- Double domain name in category URL-s
- Preserve Domain Alias
- WordPress redirects non-existing url to existing ones – how to disable
- Rewrite Preview URL to include index.php
- How to disable URL rewriting for specific URLs?
- How do I display only the parent page in the url
- How to remove wordpress directory slash
- React Router with WordPress
- How to redirect large amount of URLs?
- Redirecting /px/?q=x to /px/x/
- URL redirect on updating the post date
- Rewrite vs Redirect from ?p={ID}
- Redirect htaccess [closed]
- 301 Rewriting htaccess
- 404 redirect based on url
- How to customize sub-URLs in a wordpress website (.htaccess)
- WordPress is adding “category” word before my actual category name in url (and this is unsolicited)
- Multiple permalink with and without category for same post
- Fresh install redirects to www, breaking page loads
- Rewrite URL to get rid of wp-content etc and change URL
- taxonomy term in URL slug won’t forward to the correct term for custom post like it does for default categories/normal posts
- How to create short urls for sharing and downloadable content?
- Redirect A URL Pattern In WordPress Using .htaccess
- Migration from old CMS to WordPress 301 redirection rules?
- Changes done to .htaccess are not getting reverted back
- Allow requests to resources outside the wordpress folder
- Generic htaccess redirect www to non-www
- How do you create a “virtual” page in WordPress
- Multiple endpoints to same page
- Custom permalinks with NextGEN Gallery
- How to remove “admin.php?page=” from wp-admin using .htaccess?
- How do I add a add_rewrite_rule without it redirecting?
- Rewrite rules in .htaccess get overwritten?
- Passing & Reading URL Parameters with URL re-writing
- Taking a value from PHP_URL_PATH won’t work after WordPress 5.5 update
- Using WP rather than .htaccess to redirect pages/posts
- htaccess: Remove trailing slash from URL ending with .xml/ only
- External/non-WP rewrite rule without QSA
- Custom URL rewrite to specific page template
- Map alt domain to specific section of website w/o MU
- How to redirect http://mydomain/blog/blahblah/ to http://mydomain/blahblah/ in wordpress htaccess?
- Append a query string to the end of every URL
- Access files at new location using old file paths
- Url Rewriting a dynamic wordpress page
- Adding special characters to slug?
- a one-off rewrite rule
- Translate custom post type and taxonomy slug in URL?
- Right url for custom post type
- Preserve old website URL structure after migrating to WordPress
- Change pagination url format
- Rewriting “pretty” blog category URL with htaccess / add_rewrite_rule() causes 404 page
- Redirect a Blogger Page URL to wordpress
- How to redirect a link to a new link?
- 301 Redirect all posts urls from .html to / (without .html)
- mod-rewrite exception? keep #hash in matching urls?
- custom wordpress rewrite
- Two sets of url one content?
- WordPress .htaccess ignore path and subsequent .htaccess files in subfolders
- Custom permalink structure with %postname% in front of domain name
- Rewriting a date hierarchy into a ‘yyyy-mm-dd’ slug
- URL problem: www.sitename.com/blog and www.sitename.com/learn using same WP installation
- custom url – add attachment’s id or name after post
- Make the home page’s slug visible in the browser’s URL bar
- How to define a rule in functions.php that ONLY disables the default wp functionality that undesirably changes ‘&’ to ‘#038;’?
- dynamic URL rewrite
- Remove part from dynamic url and redirect
- How to Update / Change URL when Popup Modal Loads?
- Pretty URL via Rewrite
- url redirect none www to www
- Moved WordPress to Subdirectory, still hijacking requests to files outside of its directory
- How to add gallery slug to attachment url?
- How to remove Base URL Duplication?
- Rewrite URL query string for all pages
- ModRewrite not working properly
- WordPress post-type create url rewriting
- How to prevent URL-modification when page title contains digits only?
- How do I display a friendly URL link in the frontend?
- Please give me the rewrite rules for my ugly urls
- Spurious URL structure (Adding extra /0/s)
- WP-Include rewrite directory
- WordPress is adding pagination for all pages like www.example.com/page/123. How to remove that?
- How to change the url of the archive page to be inside the custom post type