Mapping friendly display URL and getting paraamters

Hello. This is my first post, and before posting I did search the forum and wider web, but wasn’t able to find clear direction on how to to this.

I have a function, that can be reached at this URL (am currently working on local dev):

https://example.netlify.app/.netlify/functions/hello

Untypical usage I expect to pass 3 parameters, like so:

https://example.netlify.app/.netlify/functions/hello?category=pet&type=dog&name=taco

I would love to rewrite the URL so there were no query params, and it was part of the main URL structure. So the public facing URL would be:

https://example.netlify.app/whatever/pet/dog/taco.js

And mapping the components of the URL to the appropriate variables (category, type, name).

It seems that spat and placeholders are the way here but I’ve been unable to get the basics right so far.

Suggestions appreciated.

Hey @donohoe

This is possible, and without spats or placeholders.

Take a basic function, call it path-check.js

exports.handler = async(event) => {

  return {
    statusCode: 200,
    body: event.path
  }
}

then in your netlify.toml

[[redirects]]
  from = "/blah/*"
  to = "/.netlify/functions/path-check"
  status = 200

What this does is send any path (and subpath because of the wildcard *) starting with /blah to the path-check function. That path is then available via event.path. So if you visited example.com/blah/some/path/here you would see /blah/some/path/here returned by the function.

While @coelmay has suggested something that’s correct, I’d like to add some more details:

This won’t handle the redirect. I’m assuming Coel has just tried to keep a point, but if @donohoe is going to use this example as is, it’s not going to do any kind of redirect.

Also, I’d suggest using Netlify Redirects instead of Functions for this task and as long as the expected Query Parameters are known. You can take a value of query parameter and pass it along as a path in the redirect destination.