Rewrite sub folder dynamically with country code in WordPress using PHP

I think the most difficult part is to remove the subfolder part from the url with WordPress website on both places.

What if you change subfolder name to international and change rewrite rules to have ISO codes before the urls?

So you would have:

To add the iso codes you try to do it by yourself using rewrite_rules_array filter or just use a translation plugin like Polylang that already add those codes automatically to the url and can even do browser language detection.

To do it by yourself I would do something like this: (very basic example)

function add_iso_codes_to_url( $rules ) {

    $new_rules = [];

    $iso_codes="(?:us/|uk/)?";

    // home page
    $new_rules[$iso_codes . '/?$'] = 'index.php';

    // Change other rules
    foreach ( $rules as $key => $rule ) {

        if ( substr( $key, 0, 1 ) === '^' ) { 

            $new_rules[ $iso_codes . substr( $key, 1 ) ] = $rule;

        } else {

            $new_rules[ $iso_codes . $key ] = $rule;

        }
    }

    return $new_rules;
}
add_filter( 'rewrite_rules_array', 'add_iso_codes_to_url' );