Adding a custom rewrite rule for gallery/categories page

To answer my own question, I didn’t need to remove the ‘redirect_canonical’ filter, and I only needed to change the 2nd parameter for add_rewrite_rule. The resulting function looks as follows:

function add_custom_rewrites()
{
    add_rewrite_tag('%category%', '(.+)');
    add_rewrite_rule('^gallery/([^/]*)/?$', 'index.php?pagename=gallery&category=$matches[1]', 'top');

    flush_rewrite_rules();
}
add_action('init', 'add_custom_rewrites');