Code comes from the codex:
<?php wp_redirect( get_permalink( $post->post_parent )); exit; ?>
I added the above, in an empty attachment.php page. When I visit an attachment page (eg. http://example.com/?attachment_id=2071) I get the error:
"This webpage has a redirect loop ERR_TOO_MANY_REDIRECTS"
Why?
UPDATE:
Based on the answer given, I updated my code to:
if($post->post_parent != 0):
wp_redirect(get_permalink($post->post_parent));
else:
wp_redirect(get_permalink(2));
endif;
exit;
That way if the post_parent == 0, it will go back to the home page. I don't know if I should be catching for anything else. But this seems to work.
Thanks Ignat B. for explaining what was going on. I have a few attachments that have post_parent = 0. And it just happened that the link I was trying out was one of them.