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
- Does WordPress keep track of a post’s URL history and provide automatic redirects?
- Masking wp-content/themes/name/images to just images directory using htaccess
- Use subdomain for certain urls
- Custom slug in front of search URL
- Display posts with author in the url with custom post types
- Redirecting WordPress /.htaccess / HSTS / SSL
- How to change default page slug?
- How to append a URL parameter to all outbound URLs
- Appending numbers to url do not break the link
- Custom rewrite rules are sending everything to index.php
- Sub domain redirection to taxonomy
- How to? Redirect /category/sci-fi/ to /category/scifi/?
- How to change page URLs to “www.site.com/page” instead of “example.com/blog/page” but keep post URLs as “example.com/blog/post”?
- Auto 301 to full post permalink? (using /posts/%post_id%/%postname%)
- Add_query_arg + two times the same argument?
- WordPress 404 unless there is a space in url
- Two “.htaccess” Files Located in Different Directories?
- Migrating a page to be an article
- How to get “extended” path info from URL in a plugin
- Home page with Polylang redirects to other page [closed]
- expecting 404 but redirecting to post with url subdirectory in post title
- Define orderby in url
- hard flush_rewrite_rules() not regenerating .htaccess
- Redirect home to another page with htaccess
- WordPress Redirecting Non Category Pages /page/nnn to their Canonical URLs
- How to building pretty URLs to reflect category hierarchy?
- Can I change default registration link (without htaccess)?
- A clean custom rewrite rule for weekly archives
- Add language prefix to permalinks
- URL masking of subdomain to main domain using .htaccess
- How to change the Author Slug from Username to Nickname and deal with Special Characters
- Per Page Permalink Structure for Page Post Type
- Customise particular RSS permalink
- RSS feed rewrite matching wrong rule
- Rewrite /category/cars into /topics/cars
- URL renaming issue
- Weird characters behind every single link and images
- How do I make WordPress revise an .htaccess file a certain way?
- How are rewrite rules applied in WordPress?
- Redirect users with “.” (dot) in their username and replace with “-” (dash) to correct profile
- Rewrite htacess rule doesn’t work
- Allowing multiple URLs for same Post
- How to create dynamic URLs on WordPress?
- Translatepress taxonomy and custom post type root FIX
- Param causing 404
- Rewrite Url using .htaccess or hook
- WordPress Category url redirects to subdomain
- Error 404 wordpress redirecting URL
- Remove Page Slug from URL
- How to stop WordPress creating URLs that don’t exist
- Stop wordpress to redirecting home page if no page found
- Rename page URL
- how to rewrite folder name in url to another name
- Lost WordPress website access after changing URL [closed]
- 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?