Skip to content

Commit f6b235a

Browse files
committed
Rules: Template to normalize encoded slash
1 parent f6bb29a commit f6b235a

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/content/docs/rules/snippets/when-to-use.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ You should consider migrating a Worker to Snippets if it:
535535
- Needs to run an unlimited number of times for free on a Pro, Business, or Enterprise plan.
536536

537537
Migrating to Snippets allows you to:
538+
538539
- Leverage advanced request matching via the [Ruleset Engine](/ruleset-engine/).
539540
- Eliminate usage-based billing — Snippets are [included at no cost](/rules/snippets/#availability) on all paid plans.
540541
- Simplify management by integrating traffic modifications directly into Cloudflare Rules.
@@ -567,4 +568,4 @@ Cloudflare Snippets provide a production-ready solution for fast, declarative ed
567568
Snippets and Workers solve different problems:
568569

569570
- Use Snippets for fast, lightweight traffic modifications at the edge, including header rewrites, caching, redirects, origin routing, custom responses, A/B testing and authentication.
570-
- Workers are built for advanced compute, persistent state, and full-stack applications.
571+
- Workers are built for advanced compute, persistent state, and full-stack applications.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
pcx_content_type: example
3+
summary: Create a rewrite URL rule (part of Transform Rules) to normalize encoded forward slashes (`%2F`) in the request path to standard slashes (`/`).
4+
products:
5+
- Transform Rules
6+
operation:
7+
- Rewrite URL
8+
title: Normalize encoded slashes in URL path
9+
description: Create a rewrite URL rule (part of Transform Rules) to normalize encoded forward slashes (`%2F`) in the request path to standard slashes (`/`).
10+
---
11+
12+
import { Example } from "~/components";
13+
14+
## Why normalize `%2F`?
15+
16+
Different web servers and applications handle encoded forward slashes (`%2F`) in URLs differently. Cloudflare follows [RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986), which specifies that `%2F` should **not** be automatically normalized to `/` because `/` is a reserved character in URLs, and decoding it might change the intended meaning of the path.
17+
18+
However, many origin servers **do** automatically decode `%2F` into `/` when processing requests. If your origin server behaves this way, you may want to apply the same normalization at Cloudflare’s edge to ensure consistency in request handling, rule evaluation, and logging.
19+
20+
## How to normalize `%2F`
21+
22+
To normalize encoded forward slashes (`%2F`) to standard slashes (`/`) in the request path before [subsequent](/ruleset-engine/reference/phases-list/) rule evaluation, create a new rewrite URL rule and define a dynamic URL path rewrite using [`url_decode()`](/ruleset-engine/rules-language/functions/#url_decode) function:
23+
24+
<Example>
25+
26+
Text in **Expression Editor**:
27+
28+
```txt
29+
(lower(raw.http.request.full_uri) wildcard "*%2f*")
30+
```
31+
32+
Text after **Path** > **Rewrite to...** > _Dynamic_:
33+
34+
```txt
35+
url_decode(http.request.uri.path)
36+
```
37+
38+
</Example>
39+
40+
This transformation ensures that `%2F` is always treated as `/` in the request path. This is particularly useful when setting up rules that depend on URL path matching, as it prevents discrepancies caused by differing normalization behaviors.

0 commit comments

Comments
 (0)