Netlify does not send custom headers in a redirect file (using https in the "to")

Hey guys, I’ve configured an url (which redirects to the main page as you can see). I’m using the latest version of Jekyll, and inside the root project file I have netlify.toml which basically does this:

[[redirects]]
from = “/ab”
to = “https://zerotousers.com/”
headers = {X-Frame= “Netlify543”}

Now, the problem here is that Netlify DOES NOT SEND the custom header. I’ve used Fiddler to inspect the header and didn’t see the custom header anywhere in the 301 response.

I thought it’s CloudFlare (disabled it) and the same problem persists. When I see the specific Netlify deploy, it writes " 1 redirect rule processed" but " No header rules processed"

I’ve basically followed the support topic on redirects and didn’t find anything wrong. This is a very simple test I wanted to do with custom headers, and everything is okay (the latest deploy says that 1 redirect was processed successfully), except this.

What could be the issue?:

Do you have asset optimisation enabled? I think custom headers only work without Asset Optimisation.

Yes I have, but I’d like to keep those settings (like pretty URLs). I see no reason why asset optimization settings would interfere with sending custom headers.

Could try disabling just to see if it works. Currently on my phone look around this forum I’ve seen it discussed a few times.

Quote from one of those posts:

You won’t be able to set custom headers on assets that we optimize - if you need to set custom headers on an asset (hopefully you aren’t changing the cache-control - we advise against that and it isn’t needed on our system), you’ll have to disable asset optimization and redeploy to make that work.

Can someone from Netlify confirm this?

It has been mentioned a few times as far as I can see but it would be good if it was explicitly in the docs. I’ll tag @perry for you to get a Netlify employee’s response for you :slight_smile:

Oh I see, thanks. Would be curious if I can disable asset optimization only for specific URLs (and not do it site-wide) via netlify.toml. Pretty links for example are pretty useful for things like SEO.

thanks for letting us know. We’ll get some :eyes: on this and evaluate.

Please do. I’ve been trying to get an answer for this for over a month.

Hiya @darkomkd and sorry for the delay! There is no way to process only certain URL’s; the asset optimizations are all or nothing.

However, even with NO asset optimization, we still serve your content at pretty URL’s! We’ll automatically serve:

site.com/file.html

as

site.com/file

and also

site.com/directory/index.html

as

site.com/directory

EVEN if you have processing off.

What Pretty URL’s does (in addition) is:

  1. rewrite your content to REFER to those paths
  2. upload that transformed content to cloudfront (our optimized asset CDN provider)
  3. where you cannot apply custom headers to the assets.

I’ll talk with our documentation team about where to best add these limitations to the docs, but hopefully that gives you a path forward.

Tried this. Disabled assets. Here is the redirect URL. . Still doesn’t work.

No header in the 301 redirect response, nothing. Guys, are you sure this thing works at all? If it doesn’t, at least let me know so I can decide what to do (like switching to a dynamic solution that supports custom headers).

Hi, @darkomkd. The [redirects.headers] syntax is used for headers sent to the service being proxied to. The end user doesn’t see those headers, the other web server does. With a 301 redirect, nothing is proxied so those headers do nothing.

If you want the end user to see the headers, use the normal [[headers]] syntax with one block for the redirect and another for the headers:

[[redirects]]
  from = "/abc"
  to = "https://zerotousers.com"

[[headers]]
  for = "/abc"
  [headers.values]
    X-From = "Netlify"

This returns the 301 response but with the “x-from: Netlify” header. If there are other questions, please let us know.

I’m trying to do precisely this and it doesn’t seem to be working…

I’m using netlify dev (from the latest netlify-cli@11.7.1) locally, and when I try to use [[redirects]] in tandem with [[headers]] it doesn’t work?

I’m trying to say “when somebody hits the endpoint /test, redirect them to /preferences and set a cookie”. I don’t have set-cookie as the header just yet, as I’m just trying to test it. But I don’t see any custom headers coming back. Here’s my config:

[[redirects]]
  from = "/test"
  to = "/preferences"

[[headers]]
  for = "/test*"
  [headers.values]
    X-Jim = "WORKS"

When I hit /test in the browser, I don’t see the header on the response:

Here’s a curl doing the same request:

Try as I might, I can’t seem to get my use case working with configuration in netlify.toml

Again, what I want to do is say: when a user hits endpoint /a, redirect them to /b and set a cookie. That’s it.

It’s almost like using [[headers]] with [[redirects]] don’t work for the same route?

The only way I can get this to work is to create a .html file for the intended route and do the redirect in that HTML file, e.g.

/test.html

<!doctype html>
<meta http-equiv="refresh" content="0;url=http://localhost:58802/preferences">

and then in my netlify.toml

[[headers]]
  for = "/test*"
  [headers.values]
    X-Jim = "WORKS"

That works. It serves the /test.html file with the custom header, and the HTML file contains a <meta> redirect to /preferences. I’d prefer to be able to do it via headers though so I’m not sure if this just isn’t supported on Netlify or if I’m doing something wrong…

Thoughts @luke ?

Headers are only meant for static assets and not for redirects. What you’re intending to do, is easily achievable by using Edge Functions. It provides a context.rewrite() and context.cookie` API.