WordPress Page Slug with URL custom template

Eventually I found out a solution. Below is how I have achieved this.

In my functions.php file, I have put below code.

add_filter('query_vars', 'add_user_var', 0, 1);
function add_user_var($vars){
    $vars[] = 'user';
    return $vars;
}

add_rewrite_rule('^user/([^/]+)/?$','index.php?pagename=user&user=$matches[1]','top');

This generally adds a rewrite rule if it finds user in the URL.

Now I have added a page template file name called as page-user.php and added below code to grab my query string.

$user = get_query_var('user');
echo $user;

I have created a page in backend admin with slug user and selected this template for this page.

Hope this helps.

tech