Skip to content

Commit 39bbf3b

Browse files
nikitacanopedrosousa
authored andcommitted
Rules: Add URL Rewrite Template for Normalizing Encoded Forward Slashes (%2F) (#20994)
* Rules: Add URL Rewrite Template for Normalizing Encoded Forward Slashes (%2F) * PCX review --------- Co-authored-by: Pedro Sousa <[email protected]>
1 parent 17ab92f commit 39bbf3b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
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.
15+
16+
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.
17+
18+
## How to normalize `%2F`
19+
20+
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:
21+
22+
<Example>
23+
24+
Text in **Expression Editor**:
25+
26+
```txt
27+
(lower(raw.http.request.full_uri) wildcard "*%2f*")
28+
```
29+
30+
Text after **Path** > **Rewrite to...** > _Dynamic_:
31+
32+
```txt
33+
url_decode(http.request.uri.path)
34+
```
35+
36+
</Example>
37+
38+
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)