get_query_var not working for subdirectory

You shouldn’t use get_query_var for getting the pagename (slug) – there is no guarantee that pagename will be set, depending on your permalink structure (or lack thereof).

Instead, check if the request is for a page, and then get the slug directly from the queried object:

if ( is_page() ) {
     $slug = get_queried_object()->post_name;
}