Dynamic language redirect

Hi,

I know you can redirect from = "/en/category/product/*" to = "/en/c/p/:splat",
but is it also possible to make this dynamic in case you have multiple language?

Something like from = "/:lang/category/product/*" to = "/:lang/c/p/:splat"?

I can’t find anything in the documentation: Redirect options | Netlify Docs

Except for something like from = "/category/product/*" to = "/en/c/p/:splat" condition ={Language = ["en"]}, but that’s not what I want because if I actually want to visit let’s say /nl/category/product/* I will be redirected to /en/nl/c/p/:splat.

Thanks in advance!

Hi @andylemaire, An alternative could be to use Netlify Functions to handle the Dynamic paths.

So you could create a Netlify Function to that will handle redirects from the path "/:lang/category/product/*"

netlify.toml file

[build]
  functions = "functions"
[[redirects]]
  from = "/:lang/category/product/*"
  to = "/.netlify/functions/index"
  status = 200

index.js file in your functions directory

exports.handler = async function (event, context) {

  const pathsToArray = event.path.match(/[^/]+/g);

  const lang = pathsToArray[0];

  const splat = pathsToArray
    .splice(3)
    .toString()
    .replace("", "/")
    .replaceAll(",", "/");

  return {
    statusCode: 200,
    body: JSON.stringify({ lang, splat }),
  };
};

The event object contains a property called path which is a string. The path string is converted to an array and then the zero (0) index is the language dynamic path.

The match() method is used to search a string for a match against any regular expression. If the match is found, then this will return the match as an array.

You can console log the variables to get a clear picture of the logic starting with the event object.

This is a hacky solution. I also don’t know your use case but I think it will suffice for most use cases. You can also return an HTML as your response body.

Let me know if this will help for your use case.

Thanks.