Next.js Discord

Discord Forum

Guides for redirect to app.site.com

Answered
Chalcid wasp posted this in #help-forum
Open in Discord
Chalcid waspOP
How should I approach this requirement where I have my site at site.com. And I would like for the main application to live at app.site.com. How should I approach this?
Answered by Anay-208
add 2 domains*
View full answer

39 Replies

Easiest solution is to use vercel nameservers, and add that domain to vercel i.e. app.site.com
vercel will handle the rest
you can use a permanent redirect like this: https://nextjs.org/docs/app/api-reference/functions/permanentRedirect

permanentRedirect(path, type)

Or you can route inside your domain config, that you want to redirect from site.com to app.site.com
Northern Wheatear
I think @Chalcid wasp meant a multi tenant app
Read through this post: https://vercel.com/guides/nextjs-multi-tenant-application
well, we never know when OP is not answering ^^
Northern Wheatear
Thats true lol
Wow the third person that said that 😂
Chalcid waspOP
Sorry, guys! Was night time in my time zone
I’m thinking of apps like Tinybird or WorkOS where their site.com is used for landing/marketing. And when you’re signed it, you’re redirected to app.site.com
@Chalcid wasp Yes, I do have a domain
Can you explain further what you need to do
Do you want to redirect users from site.com to app.site.com?
if that, set up a redirect in site.com to app.site.com
and use app.site.com as domain
I'll explain steps in detail if you want
Chalcid waspOP
I still want to use site.com for things like /pricing, /about, /contact, for example. However, for paths like /signin, /signup, /slug/dashboard I want them to be at app.site.com
Chalcid waspOP
Ahhhhh. Yes, yes. That does make sense
For the sake of knowledge haha, how would you approach the “difficult” way if you were to do this with one app
for 1 nextjs app
wait not wildcard
You have to create 2 domains, pointing to same app
add 2 domains*
Answer
And in nextjs app only, you'll have to redirect from app.com to app.site.com/signin, it'll be in the same app. However, if you want it such that app.com/signin doesn't work, you might need to use a condition for that, or middlewares.
Also, mark my message as a solution
its not actually difficult, but time-consuming
Chalcid waspOP
How would I test this out in localhost?
If at all possible
@Chalcid wasp How would I test this out in localhost?
You'll have to run a app twice, each on different port
in .env, set up both the urls, including values for condition
window.location.host, to see the host
here is a example condition(Could be inefficient and could have errors)
const pagesInSubdomain = ["/login"] 
//sign in page
if(pagesInSubdomain.includes(window.location.pathName) && window.location.host !== process.env.SUBDOMAIN_URI)  {
redirect(process.env.SUBDOMAIN_URI + window.location.pathName)
}
it just gives you a idea on how to achieve this
Chalcid waspOP
Ahhhh. Okay, okay. Thanks a bunch!!