Url rewrite w/o query params

I have the following url_rewrite rule to catch optional query parameters:
{
“path”: “v1/flightplan?{param}”,
“method”: “GET”,
“match_pattern”: “(.)\?(.)”,
“rewrite_to”: “Give yourself a better website » MY DOMAIN
}
with listen path:
…“listen_path”: “/avio/”,
“target_url”: “Give yourself a better website » MY DOMAIN”,
“disable_strip_slash”: false,
“strip_listen_path”: true,…

The problem is, that the query params must be in the request, otherwise this rewrite rule is not getting considered at all.
How can I construct match_pattern universally for all my endpoints (w/o query params)?

Hi @bkomac,

You can use OR logic with “|” in the match_pattern.

I also think you mean to use (.*) as (.) will capture only one character. Is that right?

“match_pattern”: “(.)\?(.)|(.*)”

Your path could then be:

“path”: “/v1/flightplan”

Hi,
Yea it’s a (.*).

I have now like this:
{
“path”: “v1/flightplan”,
“method”: “GET”,
“match_pattern”: “(.)\?(.)|(.*)”,
“rewrite_to”: “http://tyk-cp-test/vagend/DownloadServlet?$2”,
“triggers”: null
},

If I want to get query params, I have to put ?$2 at the end, but then if therare any, I get the rewrote URL like this: /vagend/DownloadServlet? ← with “?” at the end. Not wrong, but not pretty.

If all possible params are known and not too many, you can use advanced triggers to pretty it up.

{
  "path": "v1/flightplan",
  "method": "GET",
  "match_pattern": "v1/flightplan",
  "rewrite_to": "http://tyk-cp-test/vagend/DownloadServlet",
  "triggers": [
    {
      "on": "all",
      "options": {
        "query_val_matches": {
          "a_param": {
            "match_rx": "(.*)"
          }
        }
      },
      "rewrite_to": "http://tyk-cp-test/vagend/DownloadServlet?a_param=$tyk_context.trigger-0-a_param-0"
    },
    {
      "on": "all",
      "options": {
        "query_val_matches": {
          "another_param": {
            "match_rx": "(.*)"
          }
        }
      },
      "rewrite_to": "http://tyk-cp-test/vagend/DownloadServlet?another_param=$tyk_context.trigger-1-another_param-0"
    },
    {
      "on": "all",
      "options": {
        "query_val_matches": {
          "yet_another_param": {
            "match_rx": "(.*)"
          }
        }
      },
      "rewrite_to": "http://tyk-cp-test/vagend/DownloadServlet?yet_another_param=$tyk_context.trigger-2-yet_another_param-0"
    }
  ]
}

This assumes that requests would contain only one query param though.

And requests with params not defined in the triggers will be rewritten to http://tyk-cp-test/vagend/DownloadServlet i.e such params will be dropped.

See Advanced Rewriting reference in our docs here: URL Rewriting

huh, there can be x query params. Imagine service with sorting, pagination, filtering, … with query params.
I’m searching for a universal regex solution for all GET endpoints, otherwise it will strip query params off by default.

I think this should do the trick: (\??.)
{
“path”: “v1/flightplan”,
“method”: “GET”,
“match_pattern”: "v1/flightplan(\??.
)",
“rewrite_to”: “http://tyk-cp-test/vagend/DownloadServlet$1
}

1 Like

Nice putting that in the capture group :fire: