Next.js Discord

Discord Forum

how to handle a dynamic route starts with a specific prefix?

Answered
Yaman posted this in #help-forum
Open in Discord
I am migrating my app to nextjs, so I have to keep the same url routes as the old ones, however in my old app I had a path to show the user profile if url path starts with @
like:
myapp.com/@{username}
it renders the profile page of the wanted user
and if the url was:
myapp.com/whatever
it just follows the path as normal,
how can I do the same thing in nextjs?
Answered by Yaman
I found out that I should rewrites, so I did this and it worked as expected
rewrites: async () => {
    return [
      {
        source: "/@:username",
        destination: "/profile/:username"
      },
      {
        source: "/:slug",
        destination: "/pages/:slug"
      }
    ];
  },
View full answer

1 Reply

I found out that I should rewrites, so I did this and it worked as expected
rewrites: async () => {
    return [
      {
        source: "/@:username",
        destination: "/profile/:username"
      },
      {
        source: "/:slug",
        destination: "/pages/:slug"
      }
    ];
  },
Answer