Invalid request rewrite when I use URL Rewrite

Hello, I am trying to build a picture bed for my blog. The solution I use is Backblaze B2 cloud storage + Cloudflare CDN.
First, I set up a CNAME resolution to resolve one of my second-level domain names “img” to the server address given to me by Backblaze, so that when I visit
https://img.example.com/file/my-bucket-name/file/testpic.png
Will jump to
https://f001.backblazeb2.com/file/my-bucket-name/file/testpic.png
to get files.
But I found that the file link provided to me by Backblaze directly contains the name of my bucket. so that even if I use the CNAME URL through Cloudflare’s proxy service, other people can still get the real address of the file.

So I tried to use URL Rewrite in Transform Rules to modify the address.
my goal is change this link
https://img.example.com/file/my-bucket-name/file/testpic.png
to
https://img.example.com/testpic.png
to hide the bucket name.

But after I modified it, Error 1035 Invalid request rewrite appeared
Can anyone know if something went wrong with me?
Below are the rules I used

(http.request.full_uri contains "img.example.com/" and not http.request.full_uri contains "/file/my-bucket-name")

Dynamic rewrite to

concat("img.example.com/file/my-bucket-name",http.request.uri.path)

I have tried to search for errors in the forum and read the instructions but I have not found an answer that helps me.
Below is the address of the article that I have read and may be helpful to you

https://blog.cloudflare.com/introducing-transform-rules-with-url-rewriting-at-the-edge/
https://developers.cloudflare.com/firewall/cf-firewall-language/fields
https://developers.cloudflare.com/firewall/cf-firewall-language/functions

English is not my native language, so there may be some small mistakes. I’m sorry for that. If you don’t understand what I’m talking about, please feel free to ask, thank you!

Have you reviewed this documentation on how to rewrite URL path? This might help.

The expression should look something like this:

(not http.request.full_uri contains "https://img.example.com/file/mysecretbucketname/" and http.host eq "img.example.com")

(Your example would also match https://www.example.com/img.example.com/something)

And then the rewrite just rewrites the path, and does not include the hostname.

concat("/file/mysecretbucketname", http.request.uri.path)

2 Likes

I have read this documentation, but it doesn’t mention that there is no need to write the host name, and I didn’t fully understand the example at the time. Now the problem has been solved, thank you very much for your help!

Thanks for solving my problem. When reading the documentation, I did not notice that all the examples did not include the host name, so an error occurred. I have solved the problem after you pointed it out, thank you again for your help!

The actual error message is due to this config:

concat("img.example.com/file/my-bucket-name",http.request.uri.path)

The URI path must start with a /, so a minimal change of your config would look like this:
concat("/img.example.com/file/my-bucket-name",http.request.uri.path)

I just didn’t think the resulting uri.path would work as you were including the hostname in the path, so suggested it be altered also. Finally, using full_uri to match just the hostname is not efficient, and not precise enough to avoid matching requests to other hostnames.

Thanks a lot for your explanation and suggestion. In fact, the reason I used full_uri at the beginning was that I wanted to match the links below to distinguish different resources and sites

https://img.example.com/blog/something
https://static.example.com/img/something

But due to the error, I need to simplify it to try to find the reason. I will pay attention to this problem later.

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