Custom rewrite rule not working

Hope this will work out for as it did for me.

add_filter('rewrite_rules_array', 'insert_custom_rules');

add_filter('query_vars', 'insert_custom_vars');


function insert_custom_vars($vars){        

    $vars[] = 'main_page';       

    return $vars;

}

function insert_custom_rules($rules) {

    $newrules = array();

    $newrules=array(
        'page-name/(.+)/?' => 'index.php?pagename=page-name&main_page=$matches[1]'
    );

    return $newrules + $rules;
}

So, with the help of get_query_var() function, we can get value from url and use accordingly. In this way, we can get pretty URLs.

Note: If you have more than one rule, write the ones having more variables first and then those having less. Please specify static content between variables in case of multiple.