Cloudflare Docs
Rules
Rules
Edit this page
Report an issue with this page
Log into the Cloudflare dashboard
Set theme to dark (⇧+D)

Rewrite path with several URL segments to a different URL segment

To rewrite paths like /images/<FOLDER1>/<FOLDER2>/<FILENAME> — where <FOLDER1>, <FOLDER2>, and <FILENAME> can vary — to /img/<FILENAME>, create a rewrite URL rule with a dynamic rewrite of the path component:

Text in Expression Editor:

http.request.uri.path ~ "^/images/[^/]+/[^/]+/[^/]+$"

Text after Path > Rewrite to… > Dynamic:

regex_replace(http.request.uri.path, "^/images/[^/]+/[^/]+/(.+)$", "/img/${1}")

For example, this rule would rewrite the /images/nature/animals/tiger.png path to /img/tiger.png.