How to redirect folders to different domains
Answered
Japanese Bobtail posted this in #help-forum
Japanese BobtailOP
I have a project where I have to use different domains for different parts of my app. First I was thinking of copying the codebase and deploying it multiple times and then attaching domains to each deployed app. But now I was thinking of having folders in my NextJS app that each redirect to another domain.
That way I can deploy the same codebase multiple times and then attaching domains to it. Instead of copying the codebase to a new repo. I can now keep all code in 1 repo and deploy it to multiple apps.
So for example:
/pages/dashboard/welcome.js > domain.io/welcome
/pages/dashboard/about.js > domain.io/about
/pages/network/welcome.js > network.io/welcome
/pages/network/test.js > network.io/test
So as you can see the content is also different, so it doesn't have to have the same content. Just routing the subfolders to different domains.
What would be the best solution for something like this? This way I can keep the same codebase and elements but create 2 different apps.
That way I can deploy the same codebase multiple times and then attaching domains to it. Instead of copying the codebase to a new repo. I can now keep all code in 1 repo and deploy it to multiple apps.
So for example:
/pages/dashboard/welcome.js > domain.io/welcome
/pages/dashboard/about.js > domain.io/about
/pages/network/welcome.js > network.io/welcome
/pages/network/test.js > network.io/test
So as you can see the content is also different, so it doesn't have to have the same content. Just routing the subfolders to different domains.
What would be the best solution for something like this? This way I can keep the same codebase and elements but create 2 different apps.
9 Replies
Japanese BobtailOP
I think it was as easy as deploying multiple instances to multiple apps and then using redirects.
async redirects() {
return [
{
source: '/network/:path*',
destination: 'https://www.network.io/:path*',
permanent: false
},
]
}@Japanese Bobtail I think it was as easy as deploying multiple instances to multiple apps and then using redirects.
` async redirects() {
return [
{
source: '/network/:path*',
destination: 'https://www.network.io/:path*',
permanent: false
},
]
}`
yeah this is the way here. only change i would make is to use rewrites instead of redirects
deploy the app to maybe hidden.yourdomain.com
then deploy another app to the relevant subdomains, in which you rewrite <sub>.yourdomain.com/* to hidden.yourdomain.com/<sub>/*
Japanese BobtailOP
Thanks! I tried using rewrites and it works. Is it possible to get the current URL or current domain in next.config.js?
Then I could rewrite something like:
async rewrites() {
return [
{
source: '/:path',
destination: url.includes('dashboard') ? '/dashboard/:path' : '/network/:path*'
}
]
},
Then I could rewrite something like:
async rewrites() {
return [
{
source: '/:path',
destination: url.includes('dashboard') ? '/dashboard/:path' : '/network/:path*'
}
]
},
@Japanese Bobtail Thanks! I tried using rewrites and it works. Is it possible to get the current URL or current domain in next.config.js?
Then I could rewrite something like:
async rewrites() {
return [
{
source: '/:path*',
destination: url.includes('dashboard') ? '/dashboard/:path*' : '/network/:path*'
}
]
},
i don't know and i doubt you can... though in middleware you should be able to do it
all nextjs multi-tenant apps i've seen use middleware for this rewrite step
Japanese BobtailOP
Thank you for the quick answer, will look into that! 😄
Answer