Regex for Listen path matching

Hi,

We have a need to differentiate URLs based on a string present at the end of an URL

For e.g, the URLs could be like this
/aa/bb/cc/dd
/aa/bb/cc/dd/_xxx

There could be many such URLs that end with _xxx and we want to have generic config to handle them
Tried to use the following patterns in ‘listen_path’ , but no luck.

/aa/bb/{./_xxx}$
/aa/bb/{.
}/_xxx$
/aa/bb/([A-Za-z0-9-/].+)?/_xxx

Is there a way to do this based on the trailing string ?

Hi @Bala and welcome to our community.

There are some things when writing regex in Tyk

  1. The regex syntax / flavor is in the go programming language. You can use https://regex101.com/ to practice and test out regex before using them.
  2. The forward slash (/) is a part of the URL component that separates paths. There can only be one base path, but there can be multiple sub paths.
  3. You can use regex to validate multiple base paths but not sub paths

For your use case, you might want to use aa as the base or listen path, then ensure that the full path is being validated via the regex . I have attached a sample API definition below for you to start with. It ensures calls to

  • /aa/bb/cc/dd
  • /aa/bb/cc/dd/
  • /aa/bb/cc/dd/_xxx

are forwarded to. http://httpbin.org/anything/regex_success And any other calls are forwarded to http://httpbin.org/anything/regex_failure. You can find more about regex in Tyk docs.

{
  "api_id": "92c319b46cb74fc8550d5922e93b2931",
  "use_keyless": true,
  "domain": "",
  "active": true,
  "auth": {
    "disable_header": false,
    "auth_header_name": "Authorization",
    "cookie_name": "",
    "name": "",
    "validate_signature": false,
    "use_param": false,
    "signature": {
      "algorithm": "",
      "header": "",
      "use_param": false,
      "param_name": "",
      "secret": "",
      "allowed_clock_skew": 0,
      "error_code": 0,
      "error_message": ""
    },
    "use_cookie": false,
    "param_name": "",
    "use_certificate": false
  },
  "name": "Regex",
  "proxy": {
    "target_url": "http://httpbin.org/anything/regex_failure",
    "service_discovery": {
      "endpoint_returns_list": false,
      "cache_timeout": 0,
      "parent_data_path": "",
      "query_endpoint": "",
      "use_discovery_service": false,
      "_sd_show_port_path": false,
      "target_path": "",
      "use_target_list": false,
      "use_nested_query": false,
      "data_path": "",
      "port_data_path": ""
    },
    "check_host_against_uptime_tests": false,
    "transport": {
      "ssl_insecure_skip_verify": false,
      "ssl_min_version": 0,
      "proxy_url": "",
      "ssl_ciphers": []
    },
    "target_list": [],
    "preserve_host_header": false,
    "strip_listen_path": true,
    "enable_load_balancing": false,
    "listen_path": "/aa/",
    "disable_strip_slash": true
  },
  "version_data": {
    "not_versioned": true,
    "default_version": "",
    "versions": {
      "Default": {
        "name": "Default",
        "expires": "",
        "paths": {
        },
        "use_extended_paths": true,
        "extended_paths": {
          "url_rewrites": [
            {
              "path": "/bb/cc/dd/?(_xxx)?",
              "method": "GET",
              "match_pattern": "(aa/(.*)/?)",
              "rewrite_to": "http://httpbin.org/anything/regex_success/$1",
              "triggers": []
            }
          ],
          "track_endpoints": [
            {
              "path": "/bb/cc/dd/?(_xxx)?",
              "method": "GET"
            }
          ]
        }
      }
    }
  },
  "definition": {
    "enabled": false,
    "name": "",
    "default": "",
    "location": "header",
    "key": "x-api-version",
    "strip_path": false,
    "strip_versioning_data": false,
    "versions": {}
  },
  "protocol": "",
  "tags": [],
  "id": "64591cf42e6d3a0001115861"
}

@Bala This is what you may be looking for

/aa/bb{?:.*/?_?x*}

Thanks Olu, for the options. Unfortunately we can’t update the api definition to add rewrites.
The second option also did not help.

We ended adding the seperate config for all the URLs

Sorry for the late reply