Custom headers in redirect rules now passed to Functions

Hi everyone,

We rolled out an update on September 8, 2020 that changes the way we handle custom headers when redirecting to a function.

The bug

Consider the following configuration:

[[redirects]]
  from = "/hello"
  to = "/.netlify/functions/hello-world"
  status = 200
  headers = { X-From = "Netlify"}

When receiving a request for /hello we proxy the request to the hello-world function, but previously we didn’t add the X-From header to the request.

The fix

The change we made ensures that custom headers configured in redirect rules will be passed to function requests as well. Note that this change doesn’t affect you if you don’t use custom headers in combination with function redirects.

This update is available today to everyone using the non-enterprise CDN and we’ll roll out to the enterprise CDN in coming days.

Update: this has been rolled out everywhere now.

Please let us know if you have any questions!

4 Likes

Could you explain a scenario where this will be useful? Thanks in advance.

Sure, if you need to send something like an identifier to a function including context such as is described in these docs, and want to “do something different” based on a custom header you set in your redirects specification as shown in @mrdg’s code block above (e.g. function without the header is for testing and doesn’t actually process a charge; function with a header is production and should process a charge), this is now possible. Formerly, this worked only for proxying to other services; now it works for proxying from our front-end to a function you’ve deployed on a Netlify site as well.

Imagine that your site can be called by multiple names:

https://a.com/.netlify/functions/checkout
https://b.com/.netlify/functions/checkout

The code could be identical and handled by a single deploy, but before this feature was released, there was no way to pass on separate identifiers for each site (or context - such as staging). Many customers use this to “brand” a common codebase - when called via a.com, return text referring to company A, when called via b.com, return text referring to company B instead. You could handle that flow using headers like this:

[[redirects]]
  from = "/a/checkout"
  to = "/.netlify/functions/checkout"
  status = 200
  headers = { X-Brand = "a.com" }

(and so forth for your other vendors).

1 Like