Prevent image from being accessible via url as a post?

You are most likely looking at the attachment page when visiting the url. It is default WordPress behavior to create a page for every image you upload. If you don’t want these attachment pages to be accessible, you can block access to it.

To do this, edit the file attachment.php (or image.php if you want to just block access to the images) in your theme or add this file if it doesn’t exists. Add the following line of code as the first lines. All visitors of this attachment page will now be redirected to either the post where the image is connected to or, as a fallback, to your homepage.

<?php
  global $post;
  if( $post->post_parent != 0 ) {
     $redirect_url = get_permalink($post->post_parent);
  } else {
     $redirect_url = get_bloginfo('url');
  }
  wp_redirect($redirect_url);
  exit();
?>

tech