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
- How to redirect URL to a subdomain except one folder that needs to go somewhere else
- Moved WordPress to Subdirectory, still hijacking requests to files outside of its directory
- How to add gallery slug to attachment url?
- WordPress Category url redirects to subdomain
- How to remove Base URL Duplication?
- Get custom posts by segment of the URL
- Rewrite URL query string for all pages
- Any way to make Apache’s internal redirect work?
- Url wordpress rewrite
- Get parameters and custom page template
- Make URL like /fr/something display same as /something
- Pagination appearing twice in URL after rewrite
- Add ReWrite from Old Pages to WordPress (remove .php)
- ModRewrite not working properly
- Remove trailing slash
- Direct Dynamic URL in root to a location within template
- Use Parent Pages for URL Structure without Landing Page
- Fully mask *all* traces of WordPress installation subdirectory?
- Error 404 wordpress redirecting URL
- Remove Page Slug from URL
- How to retrive previous Domain URL in Wp
- different CNAME to corresponding subfolders
- .htaccess RewriteRule to include post type and taxonomy
- How To Rewrite WordPress Pages URL Only?
- URL Rewrite for CPT single posts
- URLs ending with number higher than 2147483647 redirects to to domain.com/2147483647
- URL rewrite results in a 404, but everything should work!
- Rewrite Question
- How to stop WordPress creating URLs that don’t exist
- Simple URL rewriting gone wrong
- Stop wordpress to redirecting home page if no page found
- Additional .htaccess rules based on wp page
- What is the proper way to use add_rewrite_rule to remove the (question mark)?
- Rename page URL
- WordPress post-type create url rewriting
- Can I exclude a single URL from a rewrite plugin?
- How can i maintain permalink structure and avoid a 404 error when loading external content?
- How to prevent URL-modification when page title contains digits only?
- WordPress total posts per page changed and now 404 Http error comes out [closed]
- How to exclude my home page from url of my internal pages?
- Can’t get pretty permalinks to work without index.php
- How to redirect a virtual page (e.g. /blog) to the home page?
- WordPress 3.0 Multisite – Child sites and backends appearing blank
- How do I display a friendly URL link in the frontend?
- How to rename the WordPress wp-login.php running on IIS6?
- How to change search url produced by ‘s GET method?
- how to rewrite folder name in url to another name
- Please give me the rewrite rules for my ugly urls
- Lost WordPress website access after changing URL [closed]
- Add rewrite endpoint and .htaccess
- Redirect ignore last URL segment
- Remove url parameter using wordpress
- Spurious URL structure (Adding extra /0/s)
- Rewrite Page Parameters
- Using .htaccess to redirect /da/ to / in URL
- WP-Include rewrite directory