[proxy] Rewriting nice URL part to a query param


I’m doing some URL rewrites and can’t figure out one thing.
Google easily finds me a ton of examples on how to rewrite (200 redirect) a URL like /store?id=my-blog-post to /blog/my-blog-post, meaning that the user will request and see the URL with query parameter and under the hood the content will be served from the nice URL. This is useful for ordinary redirects, but not for proxying which leverages the same mechanism on Netlify.

Basically I need the opposite. I need the user to be able to request example.com/hello/Yaroslav while the actual content should be proxied from a Netlify Function which in fact lives at example.com/.netlify/functions/hello?name=Yaroslav. I’m trying the following rule with no luck so far:

/hello/*              /.netlify/functions/hello?name=:splat       200

If you visit https://admiring-liskov-24938c.netlify.app/.netlify/functions/hello?name=Yaroslav you can see it uses the name from the query param. On the other hand the short & nice URL seems to just disregard the last part of the URL and just always responds with Hello, World: https://admiring-liskov-24938c.netlify.app/hello/Yaroslav

I’ve tried to also specify the param name explicitely instead of using :splat

/hello/:name  name=:name        /.netlify/functions/hello?name=:name       200

From what I read in other discussions on this forum, as soon as you start passing named params you have to explicitely describe them in the rule, hence the name=:name part. But that effectively renders the rule useless, Netlify just ignores such a rule. I think this rule makes the source URL require the ?name=blabla part in order to match to the destination.

Is this kind of rewrite possible using Netlify? I’d prefer to use /hello/* form and pass the :splat to the destination’s param as my actual script accepts ids which apart from letters will contain dashes and underscores and I’m not sure if /hello/:name would match these as well. Although, if it is possible to match these with the /hello/:name rule it would also work for me.

PS. This is a second part to the issue that I’ve previously described. Please also take a look at the previous one, it may give you some more context on why I need these rewrites. Netlify Function with Cache-Control

Thanks :slight_smile:

Hey @nilfalse,
Maybe I’m misunderstanding your question, but could you get the original path (the FROM part of the redirect) by referencing event.path or event.resource inside your function logic and then process accordingly?

In that case, I think the redirect would be:
/hello/* /.netlify/functions/hello 200

And function would be something like:

exports.handler = function (event, context, callback) {
  const path = event.path
  const param = path.... # write code to get only the part of the path you want

  callback(null, {
    statusCode: 200,
    headers: {
      'Cache-Control': 'max-age=365000000,immutable',
    body: 'Hello, ' + (param || 'World') + '. ' + new Date().toISOString(),

Thanks, that can be a workaround. It will require some basic string manipulation, because event.path contains the entire path, not just the :splat, but it is doable at least.

1 Like