7

Wordpress sometimes "guesses" a redirection for a nonexistent page slug if the slug is at the beginning of another URL. After some searching, my hypothesis is that this part of the canonical redirection functionality, which can be disabled using this code:

remove_filter('template_redirect', 'redirect_canonical'); 

However, I like the idea of canonical redirection, and would prefer not to turn it off entirely. Is there a way to only disable the slug "guessing"? I would prefer a 404 over an incorrect 301.

2 Answers 2

7

It's a bit hacky, but this should work:

function no_redirect_guess_404_permalink( $header ){
    global $wp_query;

    if( is_404() )
        unset( $wp_query->query_vars['name'] );

    return $header;
}

add_filter( 'status_header', 'no_redirect_guess_404_permalink' );
2
2

There is now a filter to control this functionality

add_filter( 'do_redirect_guess_404_permalink', '__return_false');

https://developer.wordpress.org/reference/hooks/do_redirect_guess_404_permalink/

2
  • For a WP newbie: how and where do I use this? (I tried to add it to wp-config.php but that failed). Commented Apr 3 at 11:37
  • Put this in the functions.php file of your theme (/wp-content/themes/[active-theme-name]/functions.php)
    – Ian
    Commented Apr 3 at 16:57

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.