When I put something after my domain name that doesn't exist I am taken to my 404 page, for example:


But if I put something that exists as an image I am taken to the page for that, for example:


How can I turn this off. I only want to be taken to pages or posts that exist.

UPDATE: This has been marked as a duplicate but the accepted answer to that does NOT fix this.

I added:

remove_action('template_redirect', 'redirect_canonical');

to my themes functions.php but the same action happens.

  • Are you talking about images in the media library?
    – s_ha_dum
    Commented Nov 24, 2013 at 16:35
  • Yes, images in the media library.
    – williamsdb
    Commented Nov 24, 2013 at 17:10
  • does it go to an image or to attachment page? Commented Nov 25, 2013 at 11:15

1 Answer 1



function no_attachment_page() {
  global $wp_query;
  if ( $wp_query->is_attachment()  ) {

add_action( 'template_redirect', 'no_attachment_page', 1 );

This should result in a 404 everytime you try to access to an attachment page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.