From 57ea4abe6d1ac95b43346b16a13a43d2ea3ba0d9 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 16:36:29 +0000 Subject: [PATCH 01/13] Cloud: rewrite rule request headers and cookies Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 68 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index 43cbd4169..b5e3e7858 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -40,7 +40,7 @@ The `request` object contains these properties: - `url` — A [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object wrapping the original request’s full URL. - `uri` — Shorthand for everything after the origin in the original request, and similar to `$_SERVER['REQUEST_URI']` in PHP. It is equivalent to `{request.url.pathname}{request.url.search}{request.url.hash}`. - `method` — The request’s [HTTP verb](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods). -- `headers` — A map of request headers and values. (Headers with dashes can be used in `destination` substitutions like any other property, e.g: `{request.headers.Content-Type}`) +- `headers` — A map of request headers and values, keyed by lowercase header name. (Headers with dashes can be used in `destination` substitutions like any other property, e.g: `{request.headers.content-type}`) ### HTTP Status Codes @@ -150,3 +150,69 @@ rewrites: ``` Headers are combined with those from the `destination`, overwriting any shared names. + +These are response headers, sent back to the browser. To change the request sent to the destination, use [`request`](#request-headers-and-cookies). + +### Request Headers and Cookies + +When a rule’s `destination` is outside Craft Cloud (anything other than our CDN or a domain that points to Craft Cloud), the gateway forwards only these headers from the visitor’s request: + +- `Content-Type`, `Content-Encoding`, and `Content-Language` +- `Range` +- `If-Match`, `If-None-Match`, `If-Modified-Since`, `If-Unmodified-Since`, and `If-Range` + +Cookies, `Authorization`, `Referer`, `Origin`, `Accept`, `Accept-Language`, `User-Agent`, the visitor’s IP address, and all other headers stay behind. To send more, add a `request` key to the rule. This example proxies a Google tag gateway path through your own domain, and forwards the Google Analytics cookies plus the visitor’s user agent, IP address, and location: + +```yml +rewrites: + - pattern: + pathname: '/dfe9{/*}?' + destination: 'https://G-XXXXXXX.fps.goog/dfe9/{matches.pathname.groups.0}{request.url.search}' + request: + cookies: [_ga, _ga_XXXXXXX, FPID, FPLC] + headers: + User-Agent: '{request.headers.user-agent}' + X-Forwarded-For: '{request.headers.cf-connecting-ip}' + X-Forwarded-Country: '{request.cf.country}' + X-Forwarded-Region: '{request.cf.regionCode}' +``` + +The `request` options only apply to destinations outside Craft Cloud. + +#### Cookies + +`request.cookies` is a list of cookie names. The gateway sends each one with the raw value the visitor’s browser sent. It leaves out cookies the browser didn’t send, and cookies with a name or value that isn’t valid in a `Cookie` header. + +This is the only way to forward cookies. You can’t set `Cookie` in `request.headers`. + +#### Headers + +`request.headers` maps header names to templates. Templates use the same placeholders as `destination`, plus two objects that only header templates can read: + +- `request.cookies`: The visitor’s cookies, by name, with the raw values the browser sent. Put names that contain dots in brackets: `{request.cookies["app.id"]}`. +- `request.cf`: [Request data from Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties), such as `country`, `regionCode`, and `city`. + +A `destination` can’t use either object, because a third party can plant a cookie in a visitor’s browser. + +A configured header replaces a default header with the same name. If a template renders an empty string (for example, because its placeholder doesn’t exist), the gateway removes the header. If a placeholder resolves to an object, like `{request.cf}`, the gateway drops the header. + +To send a literal brace, escape it as `\{` or `\}`: + +```yml +headers: + X-Location: '\{"country":"{request.cf.country}"\}' +``` + +In double-quoted YAML strings, write `\\{` and `\\}` instead. + +Rules can’t set these headers, and the gateway skips them: + +- `Host`, `Content-Length`, `Accept-Encoding`, and `Expect` +- Hop-by-hop headers, like `Connection`, `Keep-Alive`, `Transfer-Encoding`, and `Upgrade` +- `Cookie` (use [`request.cookies`](#cookies) instead) +- `Signature`, `Signature-Input`, and `Signature-Agent` +- Any header starting with `cf-`, `x-cf-`, `x-gateway-`, `x-amz-`, or `x-amzn-`, except `CF-Access-Client-Id` and `CF-Access-Client-Secret` (for destinations behind [Cloudflare Access](https://developers.cloudflare.com/cloudflare-one/identity/service-tokens/)) + +#### Caching and Redirects + +The gateway doesn’t cache responses for rules that set `request.cookies` or `request.headers`. These rules also return the destination’s redirects to the browser instead of following them. From 581632373d85faaede161c7af567d3d46d2d4eaf Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 16:57:36 +0000 Subject: [PATCH 02/13] Header placeholders aren't case-sensitive as of gateway 1.68.1 Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index b5e3e7858..178e94d66 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -40,7 +40,7 @@ The `request` object contains these properties: - `url` — A [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object wrapping the original request’s full URL. - `uri` — Shorthand for everything after the origin in the original request, and similar to `$_SERVER['REQUEST_URI']` in PHP. It is equivalent to `{request.url.pathname}{request.url.search}{request.url.hash}`. - `method` — The request’s [HTTP verb](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods). -- `headers` — A map of request headers and values, keyed by lowercase header name. (Headers with dashes can be used in `destination` substitutions like any other property, e.g: `{request.headers.content-type}`) +- `headers` — A map of request headers and values. Header names aren’t case-sensitive. (Headers with dashes can be used in `destination` substitutions like any other property, e.g: `{request.headers.Content-Type}`) ### HTTP Status Codes From ef9f6d11a1ce50165b3efd70333053408270a34c Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:07:05 +0000 Subject: [PATCH 03/13] Shorten request.headers description Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index 178e94d66..b2ddb075d 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -40,7 +40,7 @@ The `request` object contains these properties: - `url` — A [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object wrapping the original request’s full URL. - `uri` — Shorthand for everything after the origin in the original request, and similar to `$_SERVER['REQUEST_URI']` in PHP. It is equivalent to `{request.url.pathname}{request.url.search}{request.url.hash}`. - `method` — The request’s [HTTP verb](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods). -- `headers` — A map of request headers and values. Header names aren’t case-sensitive. (Headers with dashes can be used in `destination` substitutions like any other property, e.g: `{request.headers.Content-Type}`) +- `headers` — A map of request header names (case-insensitive) and values. ### HTTP Status Codes From 25232398097e5440d373cf105dffe857c8cc1051 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:09:00 +0000 Subject: [PATCH 04/13] Simplify request headers intro Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index b2ddb075d..be50866a7 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -161,7 +161,9 @@ When a rule’s `destination` is outside Craft Cloud (anything other than our CD - `Range` - `If-Match`, `If-None-Match`, `If-Modified-Since`, `If-Unmodified-Since`, and `If-Range` -Cookies, `Authorization`, `Referer`, `Origin`, `Accept`, `Accept-Language`, `User-Agent`, the visitor’s IP address, and all other headers stay behind. To send more, add a `request` key to the rule. This example proxies a Google tag gateway path through your own domain, and forwards the Google Analytics cookies plus the visitor’s user agent, IP address, and location: +To send other request headers, add a `request` key to the rule. + +This example proxies a Google tag gateway path through your own domain, and forwards the Google Analytics cookies plus the visitor’s user agent, IP address, and location: ```yml rewrites: From db48e8bdb19f936a8e7ca8215a93ded22c00e3ee Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:09:17 +0000 Subject: [PATCH 05/13] Use an obviously fake path in the tag gateway example Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index be50866a7..fa72ce307 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -168,8 +168,8 @@ This example proxies a Google tag gateway path through your own domain, and forw ```yml rewrites: - pattern: - pathname: '/dfe9{/*}?' - destination: 'https://G-XXXXXXX.fps.goog/dfe9/{matches.pathname.groups.0}{request.url.search}' + pathname: '/abc123{/*}?' + destination: 'https://G-XXXXXXX.fps.goog/abc123/{matches.pathname.groups.0}{request.url.search}' request: cookies: [_ga, _ga_XXXXXXX, FPID, FPLC] headers: From ed59514e018c7c85958261ffb08dd4d6cd412c5c Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:09:42 +0000 Subject: [PATCH 06/13] Trim cookies in the tag gateway example Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index fa72ce307..dfe16c4e5 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -171,7 +171,7 @@ rewrites: pathname: '/abc123{/*}?' destination: 'https://G-XXXXXXX.fps.goog/abc123/{matches.pathname.groups.0}{request.url.search}' request: - cookies: [_ga, _ga_XXXXXXX, FPID, FPLC] + cookies: [_ga, _ga_XXXXXXX] headers: User-Agent: '{request.headers.user-agent}' X-Forwarded-For: '{request.headers.cf-connecting-ip}' From 2318cc84d784ecfc966eb5cc7d0d452a7f7bef92 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:10:49 +0000 Subject: [PATCH 07/13] Drop redundant cookie note Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index dfe16c4e5..3c35f37b8 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -185,8 +185,6 @@ The `request` options only apply to destinations outside Craft Cloud. `request.cookies` is a list of cookie names. The gateway sends each one with the raw value the visitor’s browser sent. It leaves out cookies the browser didn’t send, and cookies with a name or value that isn’t valid in a `Cookie` header. -This is the only way to forward cookies. You can’t set `Cookie` in `request.headers`. - #### Headers `request.headers` maps header names to templates. Templates use the same placeholders as `destination`, plus two objects that only header templates can read: From 19bd501796ad3abd5890f4546c2c45fd1d43c7cd Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:11:57 +0000 Subject: [PATCH 08/13] Clarify request.headers values are templates Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index 3c35f37b8..0788952b3 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -187,7 +187,7 @@ The `request` options only apply to destinations outside Craft Cloud. #### Headers -`request.headers` maps header names to templates. Templates use the same placeholders as `destination`, plus two objects that only header templates can read: +`request.headers` maps header names to values. Values are parsed as templates, using the same placeholders as `destination`, plus two objects that only header templates can read: - `request.cookies`: The visitor’s cookies, by name, with the raw values the browser sent. Put names that contain dots in brackets: `{request.cookies["app.id"]}`. - `request.cf`: [Request data from Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties), such as `country`, `regionCode`, and `city`. From ecf35c5700474b8253d53e17322bd8d4eec707d1 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:12:45 +0000 Subject: [PATCH 09/13] Drop destination restriction note Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index 0788952b3..6a0314568 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -192,8 +192,6 @@ The `request` options only apply to destinations outside Craft Cloud. - `request.cookies`: The visitor’s cookies, by name, with the raw values the browser sent. Put names that contain dots in brackets: `{request.cookies["app.id"]}`. - `request.cf`: [Request data from Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties), such as `country`, `regionCode`, and `city`. -A `destination` can’t use either object, because a third party can plant a cookie in a visitor’s browser. - A configured header replaces a default header with the same name. If a template renders an empty string (for example, because its placeholder doesn’t exist), the gateway removes the header. If a placeholder resolves to an object, like `{request.cf}`, the gateway drops the header. To send a literal brace, escape it as `\{` or `\}`: From 1b45b97ba0fa580378578376bd9893cf80b33131 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:13:11 +0000 Subject: [PATCH 10/13] Show header template behavior as an example Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index 6a0314568..4030ff296 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -192,7 +192,15 @@ The `request` options only apply to destinations outside Craft Cloud. - `request.cookies`: The visitor’s cookies, by name, with the raw values the browser sent. Put names that contain dots in brackets: `{request.cookies["app.id"]}`. - `request.cf`: [Request data from Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties), such as `country`, `regionCode`, and `city`. -A configured header replaces a default header with the same name. If a template renders an empty string (for example, because its placeholder doesn’t exist), the gateway removes the header. If a placeholder resolves to an object, like `{request.cf}`, the gateway drops the header. +```yml +headers: + # Replaces the visitor's Content-Type header + Content-Type: 'application/json' + # Not sent when the visitor has no `_ga` cookie + X-Client-Id: '{request.cookies._ga}' + # Not sent, because `request.cf` is an object + X-Location: '{request.cf}' +``` To send a literal brace, escape it as `\{` or `\}`: From e8d57ca29585430ecc88f1e34a5b56d317fb6a94 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:13:25 +0000 Subject: [PATCH 11/13] Drop brace escape example from header templates Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index 4030ff296..51a4f2e7e 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -202,15 +202,6 @@ headers: X-Location: '{request.cf}' ``` -To send a literal brace, escape it as `\{` or `\}`: - -```yml -headers: - X-Location: '\{"country":"{request.cf.country}"\}' -``` - -In double-quoted YAML strings, write `\\{` and `\\}` instead. - Rules can’t set these headers, and the gateway skips them: - `Host`, `Content-Length`, `Accept-Encoding`, and `Expect` From 32770104d9019fbdff4c939154fc1d907ecb90a1 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:14:30 +0000 Subject: [PATCH 12/13] Fold caching and redirect note into request intro Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index 51a4f2e7e..d80b6d748 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -179,7 +179,7 @@ rewrites: X-Forwarded-Region: '{request.cf.regionCode}' ``` -The `request` options only apply to destinations outside Craft Cloud. +The `request` options only apply to destinations outside Craft Cloud. The gateway doesn’t cache responses for rules that use them, and returns the destination’s redirects to the browser instead of following them. #### Cookies @@ -209,7 +209,3 @@ Rules can’t set these headers, and the gateway skips them: - `Cookie` (use [`request.cookies`](#cookies) instead) - `Signature`, `Signature-Input`, and `Signature-Agent` - Any header starting with `cf-`, `x-cf-`, `x-gateway-`, `x-amz-`, or `x-amzn-`, except `CF-Access-Client-Id` and `CF-Access-Client-Secret` (for destinations behind [Cloudflare Access](https://developers.cloudflare.com/cloudflare-one/identity/service-tokens/)) - -#### Caching and Redirects - -The gateway doesn’t cache responses for rules that set `request.cookies` or `request.headers`. These rules also return the destination’s redirects to the browser instead of following them. From 9863961edb25ef0f698d9765ae9a77b5a159d4b7 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 4 Dec 2026 17:25:15 +0000 Subject: [PATCH 13/13] Clarify which rewrite destinations are external Amp-Thread-ID: https://ampcode.com/threads/T-e3d2b06c-fa2a-45ca-a1b6-689ee2a1489e --- docs/cloud/redirects.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/cloud/redirects.md b/docs/cloud/redirects.md index d80b6d748..9320db423 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/redirects.md @@ -155,7 +155,7 @@ These are response headers, sent back to the browser. To change the request sent ### Request Headers and Cookies -When a rule’s `destination` is outside Craft Cloud (anything other than our CDN or a domain that points to Craft Cloud), the gateway forwards only these headers from the visitor’s request: +When a rule’s `destination` is anything other than our CDN or one of the environment’s own [domains](domains.md), the gateway forwards only these headers from the visitor’s request: - `Content-Type`, `Content-Encoding`, and `Content-Language` - `Range` @@ -179,7 +179,7 @@ rewrites: X-Forwarded-Region: '{request.cf.regionCode}' ``` -The `request` options only apply to destinations outside Craft Cloud. The gateway doesn’t cache responses for rules that use them, and returns the destination’s redirects to the browser instead of following them. +The `request` options only apply to these destinations. The gateway doesn’t cache responses for rules that use them, and returns the destination’s redirects to the browser instead of following them. #### Cookies