Skip to content

Rewrite image paths with several URL segments

Create a rewrite URL rule (part of Transform Rules) to rewrite any requests for `/images/<FOLDER1>/<FOLDER2>/<FILENAME>` to `/img/<FILENAME>`.

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:

Example

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.