Tyk v2.2 Documentation Components

  1. Home
  2. Tyk v2.2 Documentation Components
  3. Rewrite a URL with the API Definition

Rewrite a URL with the API Definition

To rewrite a URL with the API Definition, you must add a new object to the extended_paths section of an API definition:

"url_rewrites": [{
    "path": "match/me",
    "method": "GET",
    "match_pattern": "(w+)/(w+)",
    "rewrite_to": "my/service?value1=$1&value2=$2"
}],
  • path: The path to match, this can contain wildcards, so to match all sub-resources under match/, you could use match/{id}. The wildcard {id} is transformed into a wide regex ((.*)) to ensure that everything possible is captured. This is then discarded. The name of the group is irrelevant, it is only for your reference.
  • method: The method to match.
  • match_pattern: This is the actual capture group to generate, this is a pure regex, in this case we are capturing two word groups.
  • rewrite_to: Each capture group you specify is designated with an index, and then made available in the rewrite_to template, here $n will map against each value found in the capture group, so in the above example, the rewrite will be:
    my/service?value1=match&value2=me
    

This can also include a new hostname, but you must specify the transport, e.g.

    https://my-new-target-host.com/my/service?value1=match&value2=me