two routes for one url
Unanswered
Xiasi Dog posted this in #help-forum
Xiasi DogOP
I have two routes /home and /dashboard. When the user is logged in I want the url "localhost:3000" to be the /dashboard route and when they aren't logged in I want it to be the /home route
17 Replies
Xiasi DogOP
hello?
European sprat
try using middleware to redirect logged in users who navigate to "/" to "/dashboard" and for not logged in redirect "/" to "/home"?
Xiasi DogOP
the problem isn't that
it's that I want to maintain the base domain name
and show either the dashboard or the home page
European sprat
there's probably some way to make it work with how you define routes and use middleware: https://nextjs.org/docs/app/building-your-application/routing/defining-routes
Just use the conditional routing
If the user is logged just use
IsLoggedIn then dashboard or else home
If the user is logged just use
IsLoggedIn then dashboard or else home
Xiasi DogOP
well if they were components that would be possible
but they are pages
I've seen it in github when the user is logged in and not
the url stays the same for both
European sprat
but you kept saying /dashboard and /home, now you're saying "url stays same for both" so i don't know what you want
Xiasi DogOP
that gives me an idea.
lemme try something
You can also use rewrites for this.
https://nextjs.org/docs/app/api-reference/functions/next-response#rewrite
I use it to display the url without the locale, for example if the user visits
https://nextjs.org/docs/app/api-reference/functions/next-response#rewrite
I use it to display the url without the locale, for example if the user visits
/en/about the request is rewritten to /about. In the same way you can rewrite /dashboard to / and your users would see the dashboard page with a clean home url in the browser.