Milo got me on the right track. If anyone else happens to have this issue the code I used is:
/**
* Redirect broken attachment links to 404
*/
function ww_404_broken_links(){
global $wp_query;
if(is_attachment()){
$attachment_path = get_attached_file( get_the_ID() );
//If the file doesn't exist on the server show 404
if(!file_exists($attachment_path)){
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
}
}
}
add_action('template_redirect', 'ww_404_broken_links');
I also added an htaccess file in the old files directory with just:
RewriteEngine off
So both the attachment posts and the direct links to the files 404 if the file is missing.
Related Posts:
- Stop WordPress from reserving slugs for media items?
- How does WordPress handle permalinks?
- Removing the redirect after changing a page’s slug
- 301 Redirects for Changed Permalink Structure & Category Base
- Is there a user-facing interface to edit an attachment’s permalink?
- How can I force WordPress to redirect to canonical permalinks?
- Subdomains to pages
- Redirect old permalinks to new permalinks
- Passing variables through permalink structure
- + Character in permalink
- Permalink redirection from ‘Day and name’ to ‘Post name’
- Nicest way to 301 Redirect traffic when changing permalink settings
- Changing pemalink structure to /%post_id%/%postname%/
- htaccess or redirect to cloak portion of a link?
- Redirect short-form URL to long-form URL (post_id to post_id + post_name)
- Auto 301 to full post permalink? (using /posts/%post_id%/%postname%)
- Media items hogging pretty permalinks
- WordPress on VirtualBox – no pretty permalinks
- Stop redirecting to posts not in a category / duplicate slugs
- WordPress 3.4 Permalinks Redirection Problem
- Redirect old permalinks with Month and name to Post name on new site
- Make attachment pages require a base url
- .htaccess – Redirect duplicated post ended in ‘-number/’ to the same url without the -number/
- Top level Pages redirecting to homepage!
- Image Attachment Url Rewrite
- How to replace “wp-content/blogs.dir” with “media” for attachment permalinks?
- WordPress redirection problem after moving to a new server?
- Adding File Extensions to Attachment Page Permalinks
- Specific URL Rewriting with parameters in wordpress
- How to 301 all posts
- WordPress within specific sub-directories, implicitly not root
- Page Attachment Permalink Structure based on Menu Order?
- Custom attachment permalink structure [closed]
- Get canonical link
- Attachment Page Permalink/URL Rewrite Issue. How to change the actual rewriting?
- Post incorrectly displaying as page
- wp_logout_url redirects to incorrect page because of pagination
- If I change permalink structures, can I use htaccess to permanantly redirect links?
- Redirect parent taxonomy to it’s child
- Does rename of page slugs create 301 redirects from old slug to new (same as for posts)?
- Custom Permalink
- Changing the post date without causing 404 error
- Change permalinks on move from one site to another
- Redirects after permalink change
- How to 301 redirect from one url to another properly
- Redirect a Blogger Page URL to wordpress
- Redirecting all posts after changing URL structure with htaccess
- WordPress 4.4 old permalink redirect
- how to redirect old blog post urls when permalinks are changed
- Htaccess https 301 problem
- Passing the JQuery string to Worldpress URL
- How to use different domains for different blogs hosted in the same host in subdomains with .htaccess
- Freeze child page URLs so parent permalink changes don’t give 404s?
- How can I make internal anchor links work with WordPress’s permalinks?
- What causes /pages/ to appear in URI, and how to remove it?
- Redirect Attachment Page to Attachment
- Some Pemalink problems, probably caused by wrong .htaccess configuration?
- Test site pages go to main site
- Remove “attachment/” from the URL of attachment pages
- Redirect From Url To Url
- I’ve removed my /author/ slug but it clashes with new and existing pages. Any fixes?
- After cloning WP do a subdirectory, I can’t access it
- change WordPress permalinks and redirect new urls 301
- Arabic permalink redirect to home page
- Change permalink to attachment
- Redirect after permalink changes
- Passing variable as permalink URL
- Blogger To WordPress Permalink Redirection
- new page redirect to old page which had the same permalink
- How do i get post thumbnail using attachment code
- Permalinks and page store
- All pages load the home page if pretty permalinks are used
- Change url of page according to my requirements in wordpress
- How to redirect old permalinks from https://exmaple.com/category/post_id to https://exmaple.com/category/post_id.html
- Double Slash After Media Path (uploads//)
- corrupted URLs – can’t leave starting page
- Find all URLs for a post
- How can i fix my permalinks?
- remove SLASH on single_post but KEEP on categories and parent pages
- where can i see wordpress automatic redirect History
- How to redirect old permalinks to ones generated by [WP-Hashed-ID] plugin?
- Code snippet to add filter pre_post_link to change Media URL (post_type attachment permalink/slug)
- Extending page urls without getting a 404
- WordPress image title attribute becomes permalink? [duplicate]
- 301 redirects after changing permalinks
- How to experiment with permalink structure on a select number of posts
- redirect any category in URL to the ID and post-name only – hundreds of pages affected
- Post redirected to page with same name but DIFFERENT permalink
- Changed Permalinks settings, how do I redirect posts on Nginx?
- Post permalinks go to the home page
- Can’t access WordPress site in subfolder possibly because of .htaccess redirect from root installation?
- changing permalink gives 404 error for old links
- All pages lead to home
- get_attachment_link() adds a hashtag to URL
- redirect old pages by .htaccess
- get_permalink to attachment not working
- My wordpress site must be being affected by outside sources [duplicate]
- Create a permalink for a post’s attachments
- Redirecting From An Old Permalink Structure
- Pages resolve at different URLs (different capitalizations)