How to configure multiple target-urls with regex?

Dear Tyk support&community,

Is it possible to set up multiple target urls under the same API?

Like a previous request, but we have many (1000+) endpoints, like this

  • api.company.com/media/news/todaynews.myservice.com/today
  • api.company.com/media/weather/todayweather.myservice.com/today

The regex based listen-path seems to match fine

  • “LISTEN_PATH”: “media/{?:[-A-Za-z0-9]+}”,

Can I use the regex group in target-url?

"LISTEN_PATH": "media/{[-A-Za-z0-9]+}",
"TARGET_URL": "https://\1.eu-central-1.instructuremedia.com/",

Can I use URL-rewriter?

  • “LISTEN_PATH”: "media/{[-A-Za-z0-9]+}",
  • “TARGET_URL”: "https://changeMe.eu-central-1.instructuremedia.com/",
  • And use url-rewriter like
    • { “path”: “.”, "match_pattern": "changeMe", "rewrite_to": "\1"}

These do not work, it seems that url-rewriter can only edit/substitute the 2nd part of the URL, but not the target-url part. How to make either the target-url or the url-rewriter work?

Regards,
Balazs

I found the docs a bit misleading, but the code/tests hinted how to make URL-host-rewite to work using UrlRewrite:

    "TARGET_URL": "https://changeMe.myservice.com/",
...
    "EXTENDED_PATHS": {
        "url_rewrites\": [ 
             { "path": ".*"
             , "method": "GET"
             , "match_pattern": "/media/([a-z]+)/(.*)"
             , "rewrite_to": "https://$1.myservice.com/$2"
             }
        ],

I could not managed to make the “trigger”… “path_part_matches” way working though.

Yes, this is possible with URL rewrite. From your last response, I assume you have seen this doc.

No, regexes only work for custom domains, listen paths and relative/sub paths.

Yes, you can. A normal URL rewrite can work however, you mention you have 1000+ endpoints so you could have a lot of rewrite rules.

The path_part match-type simply matches the components of the path itself. Each component of the path is split or separated into distinct values based on the / seperator. For example, if a path contains /foo/bar/baz, the string inserted into the value field should be either one of foo, bar or baz

I have shown an example below that triggers the advanced URL for all relative paths

"url_rewrites": [
              {
                "path": ".*",
                "method": "GET",
                "match_pattern": "/(.*)",
                "rewrite_to": "/anything/fallback_url_if_does_not_match",
                "triggers": [
                  {
                    "on": "all",
                    "options": {
                      "path_part_matches": {
                        "key_identifier": { // can be any non-spaced value but it must be used in the context trigger
                          "match_rx": "foo",
                          "reverse": false
                        }
                      },
                      "query_val_matches": {},
                      "header_matches": {},
                      "session_meta_matches": {},
                      "request_context_matches": {},
                      "payload_matches": {
                        "match_rx": ""
                      }
                    },
                    "rewrite_to": "/anything/path_part_matches/$tyk_context.trigger-0-key_identifier-0"
                  }
                ]
              }
            ]

FYI, you do have to enable context variables

Hi Olu, thanks for the reply.
It seems a bit more complicated to use path_part_matches compared to basic match_pattern.
However that might be more sophisticated and more flexible.
Could you please hint where these details are documented?

I found very minimal references to part_path_matches in Tyk docs and I’ve ended up in browsing Tyk source code along with debug logs to compose a working solution, but without this triggers+options tricks. With that regex I can cover all my 1000+ cases with one rule. Of course the actual groups must be improved/limited ( compared to “(.*)”), yet a single well formed regex can match all that I need.

Sadly we don’t have full documentation on this.

What I can do is link the sample API definition I use and a link in the source code where I got the tests from.

Hope it helps.