How to dynamically redirect user from /foo/X to /bar/X?
Since passing context for token's replace method didn't work as expected, I've defined a new token in my custom module to return the specific components of the Drupal path (using the following guide).

my_redirect.tokens.inc

&lt;?php

/**
 * @file
 * An include file defining custom tokens.
 */

use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_token_info().
 */
function my_redirect_token_info() {
  $type = [
    'name' =&gt; t('Custom URL token for redirect'),
    'description' =&gt; t('Tokens helpful for redirect pages.'),
  ];

  $node['url-arg'] = [
    'name' =&gt; t("URL Argument"),
    'dynamic' =&gt; TRUE,
    'description' =&gt; t('Returns a component of the current Drupal path.'),
  ];

  return [
    'types' =&gt; ['my_redirect' =&gt; $type],
    'tokens' =&gt; ['my_redirect' =&gt; $node],
  ];
}

/**
 * Implements hook_tokens().
 */
function my_redirect_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {

  $replacements = [];

  if ($type == 'my_redirect') {
    if ($argTokens = \Drupal::token()-&gt;findWithPrefix($tokens, 'url-arg')) {
      foreach ($argTokens as $arg =&gt; $original) {
        $current_path = \Drupal::service('path.current')-&gt;getPath();
        if ($arg === "first") {
          $value = explode('/', $current_path)[1];
        }
        elseif ($arg === "last") {
          $value = end(explode('/', $current_path));
        }
        else {
          $value = explode('/', $current_path)[$arg ?: 0];
        }
        $replacements[$original] = $value;
      }
    }
  }

  return $replacements;
}


So after applying already mentioned patches, and above code, I can use the following Redirect rule:


From: foo/*
To: /bar/[my_redirect:url-arg:2]