WP Redirect with Wild Card Characters

You could make a regex if you do not use query variables from WordPress.

Here is an example:

function loggedout_user_redirect(){

    if ( ! is_user_logged_in() ) {
        $regex = '~/users/(.*)~';
        $url   = $_SERVER['REQUEST_URI'];
        
        preg_match( $regex, $url, $matches );
        
        if ( isset( $matches[1] ) && '' !== trim( $matches[1] ) ) {
            wp_redirect( site_url( '/login' ) );
            exit();
        }
    }
}
add_action ( 'template_redirect', 'loggedout_user_redirect' );