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
/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
/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