Headers and/or redirects not working in netlify.toml

Hi,

I do not understand why my headers or redirects don’t work when I put them in my netlify.toml file but they do work when I use a _headers and/or a _redirects file.

However the build and context part does work…

This is my netlify.toml file. I’ve kept it short by deleting all headers just for testing purposes.

[[redirects]]
from = "https://dev-domain.netlify.app/*"
to = "https://dev.domain.com/:splat"
status = 301
force = true

[build]
command = "npm run build"
publish = "dist"

[build.environment]
NODE_ENV = "production"
NODE_OPTIONS = "--max_old_space_size=4096"
NODE_VERSION = "18"

[context.branch-deploy.environment]
NODE_ENV = "development"

Thanks in advance whoever can help me.

Unfortunately I can’t seem to find a fix in Redirects and rewrites | Netlify Docs.

Gr,

Andy

Hi @andylemaire can you share the site name/ID?

Hey @SamO,

ID: 0e3356e3-6bc6-4270-a8ce-1ac9b4b2aa74

Thanks! I can see that there is a “wider” redirect in place, which is interfering with the one you intend:

[[redirects]]
  from = "/*"
  to = "/.netlify/functions/server"
  status = 200

(the one you intend is just after it - but is never reached!)

I think that is a “standard” route added by our nuxt framework support, that is required, so you don’t want to remove that.

And, as to why your redirect does work when you put it in _redirects, we prepend _redirects to your configuration so it’ll show up before that default route; redirects from netlify.toml is added only at the end - after our automagic route - and is thus overridden.

This is mentioned in the docs, here: Netlify · Nitro

If you want to add custom redirects, you can do so with routeRules or by adding a _redirects file to your public directory.

Alright @fool thanks, that explains. I wasn’t aware about the Nuxt rewrite. Will solve it in the _redirects file then.

But same goes for _headers then?

Just a bump since it’s been a week and I didn’t get a reply on why the _headers aren’t working.

@fool … ? :slight_smile:

_headers is the same, yes. It’s in the docs. (Just a user who read it in the docs but doesn’t remember where).

@danielfdickinson1 correct, but I’m unable to overrule them I mean. They just aren’t working.