$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>slug - Non-existent child page URLs redirects to the image attachment URL instead of throwing 404|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

slug - Non-existent child page URLs redirects to the image attachment URL instead of throwing 404

matteradmin9PV0评论

Entering a child page that does not exist will redirect to the image attachment page, specifically, the image attachment with the same slug.

Say, entering example/cars/volvo-car/ will redirect to the image attachment page with the URL example/volvo-car/.

The volvo-car image attachment's permalink reflects the image name on initial upload, but even after I changed the attachment's slug to something different like volvo-car-image the non-existent child page still redirects to it instead of firing the 404 error page.

I can't find any entries in the database that would cause the redirect. I also tried it with different websites on different servers, but still getting the same behavior.

How can I fix it or stop WordPress from doing it?


Another thing I have discovered is that the parent page doesn't have to exist before this happens. As long as you add the image attachment URL's slug after the parent slug, it would still redirect to the attachment page like example/fasdfwefa/image-slug.

Entering a child page that does not exist will redirect to the image attachment page, specifically, the image attachment with the same slug.

Say, entering example/cars/volvo-car/ will redirect to the image attachment page with the URL example/volvo-car/.

The volvo-car image attachment's permalink reflects the image name on initial upload, but even after I changed the attachment's slug to something different like volvo-car-image the non-existent child page still redirects to it instead of firing the 404 error page.

I can't find any entries in the database that would cause the redirect. I also tried it with different websites on different servers, but still getting the same behavior.

How can I fix it or stop WordPress from doing it?


Another thing I have discovered is that the parent page doesn't have to exist before this happens. As long as you add the image attachment URL's slug after the parent slug, it would still redirect to the attachment page like example/fasdfwefa/image-slug.

Share Improve this question edited Jan 24, 2019 at 20:19 Kashif Rafique 2351 gold badge3 silver badges12 bronze badges asked Jan 24, 2019 at 17:49 rolzanrolzan 11 silver badge1 bronze badge 1
  • Firstly have you cleared your cache? – Lewis Commented Jan 24, 2019 at 20:38
Add a comment  | 

1 Answer 1

Reset to default 0

If I understood you correctly you want to disable all attachment pages and redirect them to 404 page. Place this code in functions.php and try it again.

 add_action( 'template_redirect', 'redirect_media_to_404' );
 function redirect_media_to_404() {
    if ( is_attachment() ) {
       global $wp_query;
       $wp_query->set_404();
       status_header( 404 );
       get_template_part( 404 ); 
       exit();
    }
 }
Post a comment

comment list (0)

  1. No comments so far