Transform Rules > URL Rewrite > Dynamic

Hello,

I’m trying to append a random string to the end of URLs with a set of paths and would like to know the expression to use for the dynamic query rewrite.

For example, I want to change:

example.com/path1/some-post to example.com/path1/some-post?37637763
example.com/path2/another-post to example.com/path2/another-post/75340844

If the path could be a wildcard, that would be nice, because this rewrite would apply to a set of incoming URL paths I’ve defined.

Thank you for your help.

You’ll need to use a concat() operator, e.g. concat(http.request.uri.path,"/","1234567") will result in requests to /path/ being rewritten to /path/1234567.

Thank you. When I tried that, it resulted in:

https://www.example.com/it/briefs/article-name?%2Fde%2Fbriefs%2Farticle-name%2F1234567

Strange. It’s working in my test lab with the dynamic rewrite concat(http.request.uri.path,"/","1234567").

Requests to https://www.cflare.dev/tubes.html are being received at origin to uri_path="/tubes.html/1234567.

Yeah, I get exactly this:

https://www.mywebsite.com/fr/recipes/grilled-asparagus-salad?%2Fde%2Frecipes%2Fgrilled-asparagus-salad%2F1234567

I’m assuming it’s not possible to use a random string expression in the Static rewrite to simply append the URL?

You could append the cf.ray_id field which is unique for each request.

That would work - how would I do that? I tried just putting the cf.ray_id into the static rewrite, but it returns post/cf.ray_id

You’d have to use dynamic with the following expression concat(http.request.uri.path, "/", cf.ray_id) :slightly_smiling_face:

1 Like

Same issue as before. I get:

https://www.mywebsite.com/fr/recipes/grilled-asparagus-salad?/fr/recipes/grilled-asparagus-salad/6d6c2b8ee3eb1a30-EWR

If you need to rewrite path:
concat(http.request.uri.path, "/", cf.ray_id)

If you need to rewrite query:
concat(http.request.uri.query, "&", cf.ray_id)

1 Like

Thank you very much. Now I get:

https://www.mywebsite.com/it/recipes/grilled-asparagus-salad?6d6c391b2a9a17b9-EWR**&amp**;6d6c391b40c617b9-EWR

Is the amp part removable? Otherwise, I’ll work with it this way. Thanks again.

You can replace the query with just cf.ray_id (won’t have ampersand), but that will remove any existing query parameters - is that alright?

1 Like

Yes, that would be good. Code please :slight_smile:

When I use this: concat(http.request.uri.query, cf.ray_id)

I get this: https://www.mywebsite.com/pt/recipes/grilled-asparagus-salad?6d6c461d1861f019-EWR6d6c461d3222f019-EWR

I apologize, but it is still not clear to me whether if you want to rewrite the path or the query and whether you want to replace or append. Could you tell which of the examples below describe what is is you want to achieve? Then I can post the solution :slightly_smiling_face:

Path replace:
Original: https://www.example.com/some/path?hello=world
Rewritten: https://www.example.com/6d6c461d3222f019-EWR?hello=world

Path append:
Original: https://www.example.com/some/path?hello=world
Rewritten: https://www.example.com/some/path/6d6c461d3222f019-EWR?hello=world

Query replace:
Original: https://www.example.com/some/path?hello=world
Rewritten: https://www.example.com/some/path?6d6c461d3222f019-EWR

Query append:
Original: https://www.example.com/some/path?hello=world
Rewritten: https://www.example.com/some/path?hello=world&6d6c461d3222f019-EWR

1 Like

Thank you. It’s the last one I’m looking for – query append. When I used concat(http.request.uri.query, cf.ray_id) – it appended the cfid – but it did it twice. It repeats the cfid in the rewritten URL.

This should do query append. However, if the original query is empty, the rewritten query will end up ?&6d6c461d3222f019-EWR - there’s currently no way to avoid the ampersand afaik. Though most URL parsers should just ignore the empty parameter, so I suspect it won’t cause any issues.

1 Like

Okay, it’s just that it adds the cfid twice, but it will work:

https://www.mywebsite.com/es/recipes/grilled-asparagus-salad?6d6c67fe796f3342-EWR&6d6c67fea1b63342-EWR

That is… weird :thinking: I’m having no such issue while testing this on my end. Do you have multiple Transform Rules?