I am trying to replace certain characters in requested URLs and then send a redirect to the modified URL. I have tried to use Netlify functions, but it seems the deployment of these only works when a git repo is connected. We currently use only manual deployments.
I have also tried to use a _redirects file. This works, but doesn’t cover all my use cases (only some using “:splat” where the path does not change); specifically it doesn’t seem to have a way to dynamically modify the requested URL.
So, how can I replace characters in URLs in a manual deployment setup? Thanks!
Thank you, @nathanmartin. I have managed to deploy the function now using the CLI.
However, I am still stuck on the actual task. The char replacement function is being triggered by adding this to the _redirects: /* /.netlify/functions/replacechars 200
I am wondering, if this wouldn’t perhaps lead to an infinte loop of redirects, because the script is called not only once.
Secondly, I am not able to extract the host from the request headers directly, which is odd.
A related question: Constructing the redirect response using the Netlify Response object does not work either. This is the code snippet:
// Redirect to the modified URL
return new Response('', {
status: 301,
headers: {
"Location": modifiedUrl
}
});
This leads to the following error:
This function has crashed
An unhandled error in the function code triggered the following message:
TypeError - import_functions.Response is not a constructor
Is this a special Response object and if yes where can I find documentation? Or is it this standard one?
Final issue: Using Netlify _redirects, how I can prevent requests being touched more than once by the function?
This is how requests are being routed through the function:
/* /.netlify/functions/replacechars 200
I know I could probably add some request param to modified URLs, but maybe there is a better way.
The crux of it being that you can’t use a single catch all rule of /* to push everything to a function, then expect that the 301’s returned to the user not also be caught by that same catch all when the users browser then requests them.
If you’re happy to have the function execute on every request, then you could have it return either 301 or 200 (and the content), depending on what it has performed on the URL.
Thanks a lot, @nathanmartin. I figured out another way. Fortunately, the URLs to be redirected have a common root and this common root is not present in the redirect targets. So, instead of redirecting /*to the replace function, I can apply redirects to /commonRoot/*. Then, in the replace function I also change the commonRoot to a modifiedRoot and then do the remaining redirects based on this modifiedRoot. Thus, I am able to prevent an infinite redirect loop. This only works in my special case, but that’s good enough for now. (“The perfect is the enemy of the good”, said Voltaire)
Excellent, I was going to mention that usually when people are redirecting there is some common initial path fragment they can use to more easily identify origin/destination, but your use of /* seemed fairly intentional.