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!
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.