How to rewrite URL back to default custom URL if no destination found

My current situation is:

I'm redirecting the custom category request to below kind of url:

http://my_url/news/category/abc

I've managed to add my custom query var category and can get the query var now. The hook is as below:

add_filter('rewrite_rules_array', function ($rules) {
    return
        ['^news/?$' = 'index.php?post_type=newspage=1category=all'] +
        ['^news/category/(.+)/?$' = 'index.php?post_type=newscategory=$matches[1]'] +
        $rules;
});

Url like http://my_url/news/category/abc now goes to archive-news.php and list all posts which has custom category abc.

And url like http://my_url/news/lalala goes to single-news.php and display full content of post with title lalala.

My problem is:

when url is http://my_url/news/category/ it goes to an empty page which I believe is t he 404.php.

Seek solution

I'm think about how to redirect it to http://my_url/news in my hook so it will automatically picks all as its default category.

All practical solutions are welcome. Thanks in advance.

Topic rewrite-rules url-rewriting Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.