URL rewrite help

What is the name of the domain?

website

What is the issue you’re encountering

Trying to setup a URL redirect

What steps have you taken to resolve the issue?

I’m trying to convert a simple URL rewrite rule to cloudflare now we are using R2 image storage but I can’t quite work it out. Can anyone help out?

I’ve got a URL for small preview images: https://r2.website.com/images/media/artist/thumb/wxstuy1447602884.jpg/small

and I want to rewrite to
https://r2.website.com/images/media/artist/thumb/small/wxstuy1447602884.jpg

I’ve managed to use trace to check the match rule works:
https://r2..com/images/media////small

I’m guessing I need to do a dynamic query to rewrite the URL but tried a number of ways that have not worked. Its really hard without any logging on where the request is actually being sent like I can do with a local webs server.

Can anyone help out?

This is the IIS rewrite rule I am trying to replicate on cloudflare.

I am on the Pro Plan.

We have plans to improve Trace and make testing much easier next year. For now, I would suggest to create your URL Rewrite rule as a Single Redirect in a Custom filter expression mode. This way you can validate your syntax simply by confirming the redirect target is correct.

Now, Business and Enterprise plans would normally use regex_replace() to achieve the rewrite you need. However, on a Pro plan currently you will have to combine multiple transformation functions to make it work:

  1. Narrow down the static part of the URL path and calculate its length (i.e. /images/media/artist/thumb/ is 27 characters)
  2. Use substring() function to return the dynamic part of your URL (i.e. wxstuy1447602884.jpg/small) and trim the required number of characters from the right (i.e. /small is 6 characters)
  3. Use concat() function to assemble the new URL together

In the end, you will have a rule similar to this:

  • When incoming requests match…: (http.request.full_uri wildcard "https://r2.website.com/images/media/artist/thumb/*/small")
  • Type: Dynamic
  • Expression: concat(substring(http.request.uri.path, 0, 26), "/small/", substring(http.request.uri.path, 27, -6))

This expression will rewrite /images/media/artist/thumb/wxstuy1447602884.jpg/small to /images/media/artist/thumb/small/wxstuy1447602884.jpg

2 Likes

Many thanks for the most insightful reply. I tested and it worked, but because my different URL wildcard folders are not a constant length, it doesn’t really scale to all my image types without creating loads of rules. At least its something though! I’m now able to see my preview images on my web service using R2 storage.

Trace definitely needs some improvement to help with debugging. I would never have got that solution in a million years even though I have read the documents and examples many times.

On a more general note, I am sure you are aware already, but the vast majority of end users like myself are never going to want to use regex, substring or concat. It seems absurdly over engineered to require those for simple URL redirection when Apache, IIS etc solved this 20 years ago with a simple wildcard syntax. Hopefully the product manager will read this, Keep it simple please :wink:

2 Likes

We have just released a simple UI for redirects, and we’re not planning to stop there. Stay tuned. :wink:

2 Likes

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