301 redirect instead of 404 when URL is a prefix of a post or page name?

This is normal, it is because redirect_canonical(), which makes sure you are always at a “canonical” URL (conforming to your permalink structure), executes redirect_guess_404_permalink() to make a best guess at a post when the URL is incomplete. If you want to prevent this, I think the best way is to hook into the redirect_canonical filter, and return false if it is a 404. Something like this:

add_filter('redirect_canonical', 'no_redirect_on_404');
function no_redirect_on_404($redirect_url)
{
    if (is_404()) {
        return false;
    }
    return $redirect_url;
}

Leave a Comment