Next.js Discord

Discord Forum

Redirect in next.config

Answered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
hello , i use this code to redirect dynamic segment page in my root to the same page but with searchParams . problem is this code also apply to all other pages that are not dynamic . how this work ?

async redirects() { return [ { source: "/:id", destination: "/:id?name=test", permanent: true } ] },
Answered by James4u
okay then I think you need to add redirecting logic in your page not in the middleware
View full answer

39 Replies

@Barbary Lion Yeah, your current solution will be adding query param to all other pages
what exactly do you want to implement?
Barbary LionOP
i want this redirect to only apply to [id] page . but it apply to all pages, be dynamic segment or not . for example i have a page named "agents" in app directory . this redirect effect this too . shouldnt this only apply to pages with [id] name ?
can you show me your directory structure?
Barbary LionOP
Hmm I never used that kind of dynamic routes
so what can id be for your dynamic route?
I would recommend put [id] dynamic routes inside a route segment
/another-segment/[id] not /[id] as long as you have agents, city, and other pages in the level
Barbary LionOP
id is a name .actually it doesnt matter . problem is that redirect should only apply to [id] page .
@James4u I would recommend put `[id]` dynamic routes inside a route segment
Barbary LionOP
yes that could work i try it
async redirects() {
    return [
      {
        source: "/segment/:id",
        destination: "/segment/:id?name=test",
        permanent: true
      }
    ]
  }
Then update your next config like above
@Barbary Lion Try this out and mark solution if it works. πŸ™‚
Barbary LionOP
did you meant route groups ? cause i couldn't get it to work in redirect . if i put like this app crash . /(agent-listings)/:id .
no, route group is a different thing
just a normal segment
/agent-listings/:id
Barbary LionOP
ahh no then i need this to be in root of group .
let say your app is https://example.com
then if I hit https://example.com/agents
should it be /agents or [id]
what do you think? what's the reason to have dynamic routing in the root level and other pages there?
Barbary LionOP
that should be agents . but if put any other text that should go to [id] .
@James4u what do you think? what's the reason to have dynamic routing in the root level and other pages there?
Barbary LionOP
i know its a bit weired but thats out of my power to change πŸ˜…
I see, maybe your boss asked it.
okay then I think you need to add redirecting logic in your page not in the middleware
Answer
Barbary LionOP
yes that seems to be the only solution . but that come with it disadvantage . like it flash a empty page at first . but thats not my problem anymore 😁
well, are you using app router? if it's a server component, server side redirection will be perfect
no glitches at all
Barbary LionOP
yes thats app router
why flash an empty page then?
Barbary LionOP
i mean it show header and footer and then do the redirect .
should i redicrect put that in layout ? not sure
can you show me the code?
Barbary LionOP
if(!searchParams.agentName) {
redirect(/${params.id}?name=${name})
}
okay then is your footer and header inside root layout.tsx?
Barbary LionOP
yep
Okay then it's expected
what about adding loading UI?
Barbary LionOP
hmm let me try