Search code examples
httphttp-redirecthttp-status-code-301cache-controlnetlify

Netlify: can I have a 301 redirect with custom HTTP headers?


Using Netlify, can I add custom HTTP headers to a path on one domain but not another?

e.g. if I wanted my-domain.com to return a 301 with headers different to the headers returned by www.my-domain.com.

@talves has explained below why my initial approach (quoted) won’t work.

Given the below in netlify.toml, I expect to see my custom Cache-Control header (and NOT see the Link header) when I run curl -I http://hello-yoga.co.uk but that's not happening.

What am I doing wrong?

[[headers]]
  for = "/"
  [headers.values]
  Link = "</yoga-people.svg>; rel=preload; as=image"

[[redirects]]
  from = "http://hello-yoga.co.uk/*"
  to = "https://www.hello-yoga.co.uk/:splat"
  status = 301
  force = true
  headers = {Cache-Control = "public, max-age=2592000"}

Result:

$ curl -I http://hello-yoga.co.uk
HTTP/1.1 301 Moved Permanently
Cache-Control: public, max-age=0, must-revalidate
Link: </yoga-people.svg>; rel=preload; as=image
Location: https://www.hello-yoga.co.uk/

Solution

  • Netlify support confirmed the only way to do this is with two Netlify apps:

    1. For the www subdomain
    2. For the apex/root domain