Obviously fly.io supports redirect from http to https, but is there really no other redirects possible?
Before having looked at the documentation, I would have expected I could use http (and https) handler to redirect from one domain to another, it does not look like this is possible, I can imagine the syntax being something as easy as:
In our setup, both old and new DNS entries currently point to the fly instance but since we can only realistically support one domain via Oauth callback then it would be good to do a standard redirect on the old url to the new.
Unfortunately our dns provider only supports 301 redirecting the whole domain, not just a specific subdomain, otherwise we could do this via that avenue, and since the application is nextjs then it is a pain to do it internally.
Is the only viable solution really to make a nginx proxy or something to handle this? Seems unwieldly!
Our proxy / http doesn’t really have a rules setup yet. We’re working on it, and it’s possible we’ll bake in redirects, but that doesn’t help you right now.
You definitely don’t want to be adding proxies, though.
I’m surprised NextJS makes this so hard. The app is really the best place to handle this kind of thing.
Those don’t let you match on hostname, or even redirect to a different hostname. I think you’d need to handle HTTP redirects in the server.js or equivalent file before passing off to NextJS.
@alasdairwilson I don’t think that’s all that complicated, though, will you share your server.js file?
I think you can configure it to look at the hostname via the headers, eg:
// if the host is `example.com`,
// this redirect will be applied
{
source: '/:path((?!another-page$).*)',
has: [
{
type: 'host',
value: 'example.com',
},
],
permanent: false,
destination: 'https://google.com', // I'm not 100% sure next will let you go to another domain
If that doesn’t work, then you can redirect in the middleware.ts file, I currently do this if the hostname is *.fly.dev => redirect to example.com
Oh nice! I looked at middleware and couldn’t figure out how to get it to do a redirect to anything but a path. Is that the NextRequest.redirect() thing?
I’d say part of the motivation is I manage the repo and the primary deployment but there are other deployments of the same app and it is a pain to have deployment specific code in the repo that isn’t used by forks.
This looks like the most promising answer, I had thought since redirect doesn’t work, indeed I thought next was actually completely unaware of the host address but I guess, as you say, we can see the host domain in the header.
I’ll have a go on monday, thanks all who suggested something, I was thinking of just putting an express service or nginx infront of it but that is hardly ideal.
So for anyone that comes here @khuezy solution is the way to go (so, thank you), I did it in next.config.js rather than server.js but the idea was identical.
I would still say it would be good to have this in fly, as I would have rather not have this deployment specific code in the repo but I get it was a unique circumstance with us being unable to use our dns provider to do the forward for me.