Custom headers for URL

I would like to add custom headers for all pages under a specific hostname.

I am testing with

I currently have in my _headers file the following:*
X-Robots-Tag: noindex
Test: True

I cannot get the X-Robots-Tag header to work. The “Test” header works as expected. Is this the correct syntax? The documentation does not have a URL example, only a path example. Thank you in advance!

You can also get the same behaviour by adding a simple meta tag:

<meta name="robots" content="noindex" /> or use robots.txt to block the page from SEO.

Thank you for the reply. I understand you can get the same behavior via the meta tag, but I wanted to know if it is possible to do this through custom headers. Let’s say for example I wanted a different custom header.

Is it possible to set specific headers for all pages under a specific domain?

Have you read this documentation? :slight_smile: I typically use the [[headers]] syntax in the netlify toml it feels easier to read

Thanks for the suggestion. I read this doc and tried multiple things before posting - apologies probly should have included all the things I tried. This documentation does not provide a URL example, only path examples.

I currently have this saved for the same site and it does not work as I intend

for = “*
Success: True

Test: True

If that is not the correct syntax can you please provide a working example?

I’d imagine you need to use /* rather than the absolute URL in the for. Can you try that?
Seen here:

/* works, as you can see on the live site the response header includes Test: True.

The problem is that will apply to all URLs regardless of the the hostname.

I want to know if it’s possible to do it for a specific hostname.

For example has one set of headers returned for all pages under it and has a different set of headers returned for all pages under it. I want them to have different headers returned through one _headers config file. The staging subdomain in my example would be the branch subdomain and the www subdomain would be my primary subdomain.

Is that possible? Let me know if I can clarify.

Ah hmm I think there was some confusion, I thought you WERE looking to apply the headers globally for all URLS. Thats what the [[headers]] in the netlify.toml will do.

I don’t believe there is a way to set them for specific URLs but you can set headers to be different based on their deploy context if that is of any help. IE: Production, branch deploys etc

If you create multiple _headers files for your contexts you could then do something like this inside your netlify.toml to inject those headers for that context IE staging could be branch deploy etc.

  command = "yarn build && cp static/_branchdeploy_headers public/_headers"

  command = "yarn build && cp static/_deploypreview_headers public/_headers"

Thank you for the continued support. You understand my goal and the deploy context approach would definitely accomplish what I want to do! I’m using a static website so I wasn’t sure how to implement your exact approach but I tried the following.

        for = "/*"
            Test = "True"

        for = "/*"
            Test = "false"

The current problem is that both my master branch (i.e. production branch) and my staging branch are returning “false” as the value. Is there something wrong with my syntax here? Ideally I’m trying to have one netlify.toml file for both branches.

1 Like

Nevermind, I was able to get a working solution with your guidance. Really appreciate your example!

For those using a basic static site the solution for me was the following.

In the root directory I created two files

In each file I add the the headers I wanted

Test: False (True for staging for testing)

Then in the root directory I created a netlity.toml file with
command = “cp _production_headers _headers”
command = “cp _staging_headers _headers”

This works as expected. On the production branch Test returns True and on the staging branch Test returns false.

It would be really nice if you could manage it all in one TOML file like I was trying in my previous comment, but it does not seem like that is possible with headers.

1 Like

Glad we sorted it! Yeah netlify toml headers are global so I don’t think there’s a solution there :blush:

1 Like