Multiple query vars sorting combination and url rewrite

Well, I explain here what I finally did to sort this out. I haven’t found a proper way to do so but:

  1. I created a rewrite function that retrieves all parameters and generates a final URL with the parameters ordered in a specific way.

    function sc_change_programs_search_url_rewrite() {
        if(get_query_var( 'post_type' ) == 'program') {
            if(isset($_GET['word_search']) || isset($_GET['os']) || isset($_GET['category']) || isset($_GET['archived'])) {
                $available_query_vars = array( 'word_search' => 'p', 'sistema_operatiu' => 'so', 'category' => 'cat', 'archived' => 'archived' );
                $params_query = '';
                foreach($available_query_vars as $query_var => $key) {
                    if (get_query_var( $query_var )) {
                        if($query_var == 'archived') {
                            $params_query .= $key . "https://wordpress.stackexchange.com/";
                        } else {
                            $params_query .= $key . "https://wordpress.stackexchange.com/" . get_query_var( $query_var ) . "https://wordpress.stackexchange.com/";
                        }
    
                    }
                }
    
                if( ! empty( $params_query ) ) {
                    wp_redirect( home_url( "/programs/" ) . $params_query );
                }
            }
        }
    }
    
  2. I created all the rewrite rules (given that the order can be only one, there are less rules to implement, but there are quite a few anyway):

    $aNewRules = array(
        'programes/p/([^/]+)/so/([^/]+)/cat/([^/]+)/arxivats/?' => 'index.php?post_type=programa&cerca=$matches[1]&sistema_operatiu=$matches[2]&categoria_programa=$matches[3]&arxivat=1',
        'programes/p/([^/]+)/so/([^/]+)/cat/([^/]+)/?' => 'index.php?post_type=programa&cerca=$matches[1]&sistema_operatiu=$matches[2]&categoria_programa=$matches[3]&arxivat=0',
        'programes/p/([^/]+)/so/([^/]+)/arxivats/?' => 'index.php?post_type=programa&cerca=$matches[1]&sistema_operatiu=$matches[2]&arxivat=1',
        'programes/p/([^/]+)/cat/([^/]+)/arxivats/?' => 'index.php?post_type=programa&cerca=$matches[1]&categoria_programa=$matches[2]&arxivat=1',
        'programes/p/([^/]+)/so/([^/]+)/?' => 'index.php?post_type=programa&cerca=$matches[1]&sistema_operatiu=$matches[2]&arxivat=0',
        'programes/p/([^/]+)/cat/([^/]+)/?' => 'index.php?post_type=programa&cerca=$matches[1]&categoria_programa=$matches[2]&arxivat=0',
        'programes/p/([^/]+)/arxivats/?' => 'index.php?post_type=programa&cerca=$matches[1]&arxivat=1',
        'programes/p/([^/]+)/?' => 'index.php?post_type=programa&cerca=$matches[1]&arxivat=0',
        'programes/so/([^/]+)/cat/([^/]+)/arxivats/?' => 'index.php?post_type=programa&sistema_operatiu=$matches[1]&categoria_programa=$matches[2]&arxivat=1',
        'programes/so/([^/]+)/arxivats/?' => 'index.php?post_type=programa&sistema_operatiu=$matches[1]&arxivat=1',
        'programes/so/([^/]+)/cat/([^/]+)/?' => 'index.php?post_type=programa&sistema_operatiu=$matches[1]&categoria_programa=$matches[2]&arxivat=0',
        'programes/so/([^/]+)/?' => 'index.php?post_type=programa&sistema_operatiu=$matches[1]',
        'programes/cat/([^/]+)/arxivats/?' => 'index.php?post_type=programa&categoria_programa=$matches[1]&arxivat=1',
        'programes/cat/([^/]+)/?' => 'index.php?post_type=programa&categoria_programa=$matches[1]',
        'programes/arxivats/?' => 'index.php?post_type=programa&arxivat=1',
    );
    $aRules = $aNewRules + $aRules;
    return $aRules;
    

Still hope there is a better solution…