URI redirect rule

I’m trying to create a dynamic redirect rule for a specific domain. I understand regex, but not how it is implemented w/in Cloudflare.

I want to match on this regex to get the different groupings of a specific URI:

and I want to redirect to this expression:
where {C:5} is the 5th (last) group of the matched regex above. It seems like instead of {C:5} I should be using ${5} according to documentation, but that doesn’t work.

I am getting this error:
'regex_replace(http.request.uri.path, "https://www.mywebsite.com/Document/File?id=${5}")' is not a valid value for target_url because the expression is invalid: Filter parsing error (1:117): regex_replace(http.request.uri.path, "https://www.mywebsite.com/Document/File?id=${5}") ^ invalid number of arguments (Code: 20083)

I’ve also tried adding the full expression in the Expression field with no luck:
regex_replace(http.request.uri.path, "https://www.mywebsite.com/Document/File?id=${5}")

How specifically can I grab the query string of the request, change the URI, and add the same query string to redirect to the new URI with the same query string? The problem is the domain and path both change, only the query string remains the same.

Hi there,

You can simply check thePreserve query string box:

This example:

(http.host eq "mydomain.com" and starts_with(http.request.uri.query, "id="))

It will only redirect requests for the host mydomain.com (not www.mydomain.com or any other subdomain)if the query string exists and starts with id=, and redirects them to https://myotherdomain.net keeping the same query string.

So as an example if I were to go to https://mydomain.com/somepath?id=123 I would be redirected to https://myotherdomain.net?id=123

Take care.

Thanks for the direction. I now have this working as required. This was very helpful.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.