Using url rewrite to append to path
I want to use a url rewrite rule to append /files before certains paths
How would i go about doing this
19 Replies
concat("/files", http.request.uri.path)to be used something like this:
becomes
https://yourhostname.com/files/specialpathorsomething/ttt
ah I see, the documentation is a bit spare
is there any good resource/documents that shows off url rewrite?
there's examples: https://developers.cloudflare.com/rules/transform/url-rewrite/examples/
and a cheat sheet a community mvp made here: https://community.cloudflare.com/t/transform-rules-cheat-sheet/549549
Cloudflare Docs
URL rewrite examples · Cloudflare Rules docs
The examples in this page illustrate how to rewrite URLs with Transform Rules.
really though I think it boils down to two things:
The matching is the normal ruleset engine, nothing too crazy, just breakdown your expression as much as possible, almost never using the full url fields, instead hostname, path, and query string functions, with starts_with/ends_with/contains/equals.
For replacing, you don't really have too many options. regex_replace is business plan or above which is a bit limited. They are working on a more flexible page rules like regex engine but nothing yet. As it is right now, concat and substring are your two most useful functions, and docs of all the ruleset fields here: https://developers.cloudflare.com/ruleset-engine/rules-language/fields/
you can substring negative either way, if you wanted to get the subdomain for example just substring from 0 to negative the amount of chars in your domain. Same for getting bits of a path, if you know it's a constant length at the end or the start you can grab from there.
Cloudflare Docs
Fields reference · Cloudflare Ruleset Engine docs
The Cloudflare Rules language supports a range of field types:
i so i cant use regex_replace on pro?
okay no worries
but thanks for sharing all these resources
i can use other funtions right? such as to_string?
only business plan or ent
i can use other funtions right? such as to_string?yes
alright thank you 👍
slightly off topic, but i cant seem to find a general chat to ask this in
i cant seem to find brottli where it used to be (Content Optimization)
any idea why?
because it's forced on now
https://community.cloudflare.com/t/important-brotli-on-by-default-and-brotli-toggle-removal/652757?u=chaika
oh thats great
thanks
@Chaika if i bought the WAF addon id get this right? like it says in the dashboard
if i wanted to use the WAF addon for just this would i get different pricing, cuz $200 for url rewrites is a bit outta my budget 😅
What do you mean "WAF Addon"? Are you talking about the Error message saying that you need either Business or Enterprise w/ a specific WAF addon?
yeah, the WAF that isnt zone specific
Enterprise only
that would be way more expensive then just Business lol
oh alright
ig business it is
you need regex_replace?
if so, why not just use Page Rules for now for redirects, and CF has already said they're going to build a better regex system for free before they take Page Rules away
okay ill give that a shot
https://developers.cloudflare.com/rules/page-rules/how-to/url-forwarding/#advanced-forwarding-options
yea, page rules has pretty flexible replacement,
http://*.example.com/*
-> http://$1.example.com/$2
or whatever you needCloudflare Docs
URL forwarding | Page Rules · Cloudflare Rules docs
Page Rules allow you to forward or redirect traffic to a different URL, though they are just one of the options provided by Cloudflare.
although those are proper redirects and not just transparent rewrites
yeah i understand, but that might just be enough for me
@Chaika i managed to do it with url rewrites using substring and concat
it works great
thank you so much for all the help