0

I am interested in createRedirect actions.

I would like to utilize this to redirect user in specific country with particular language, and give english for unspecified country.

createRedirect({
  fromPath: `/`,
  toPath: `/id/`,
  conditions: {
    country: "id",
  },
});

createRedirect({
  fromPath: `/`,
  toPath: `/en/`,
});

I use gatsby-plugin-meta-redirect to make the redirect possible.

I then deploy the preview through netlify. When I tested for non id country using VPN, it still redirect to /id/ instead of /en/.

The question is How to make a default redirect for non specified country?

satriahrh
  • 37
  • 1
  • 7

1 Answers1

0

Redirects like the one you are suggesting only works in Gatsby Cloud, as the docs points out:

createRedirect

Create a redirect from one page to another. Redirects work out of the box with Gatsby Cloud. Read more about working with redirects on Gatsby Cloud.

To deal with redirects in Netlify you need to create a _redirects (without extension) file in the /static folder of your project, as it needs to be published in the public folder. Because you need more complex redirection the workaround is slightly different: in this case you need to create a netlify.toml file at the root of your project with something like:

[[redirects]]
  from = "/"
  to = "/id"
  status = 301
  force = false
  conditions = {Language = ["id"]}

[[redirects]]
  from = "/en"
  to = "/"
  status = 301
  force = false

You can read more details about the parameters and its values in the docs: https://docs.netlify.com/routing/redirects/#syntax-for-the-netlify-configuration-file

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • let say that I will use the method you are suggesting, did it also answer my question? will that be redirect to `/en` for non `id` country, @Ferran ? – satriahrh Jun 23 '22 at 13:42
  • 1
    Test it, and you will see, but it should. Your trials are doing exactly the same, but the order of the rules will matter, so try switching them – Ferran Buireu Jun 23 '22 at 13:54