How to prefix custom post type URL with custom post taxonomy term?

Ok, found the (a?) solution.

"rewrite" => array( "slug" => "activity", "with_front" => false ),

and

function my_injecter_agence_in_activite_url( $post_link, $id = 0 ) {
    $post = get_post( $id );

    if (  $post->post_type == 'activity'  ||  'publish' == $post->post_status  )
    {
        $terms = wp_get_object_terms( $post->ID, 'agence' );
        if ( $terms )
        {
            $post_link = str_replace("activity", $terms[0]->slug.'/activite', $post_link);
            return $post_link;
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'my_injecter_agence_in_activite_url', 1, 3 );


function my_rewrite_rules()
{
    add_rewrite_rule(
        '(.*)\/activity\/(.*)?$',
        'index.php?post_type=activity&name=$matches[2]',
        'top'
    );
}
add_action( 'init', 'my_rewrite_rules' );

I was pretty close. I think there was a problem with my initial str_replace.

Now, neither posts nor pages or activities are broken, everything seems ok.

The custom taxonomy term used as prefix in the URL is purely cosmetic and I don’t have disambiguation if two activities have the same slug, but I think it’s not supposed to happen as WP won’t let me have two posts with two identic slugs.

Now I just have to find a way to make sure I get a 404 error if looking for an activity in the wrong term (for example, if trying to reach example.com/new-york/activity/bike-tour-in-miami)

If someone has a better way of doing this, please let me know.

error code: 523