Next.js Discord

Discord Forum

Rewrite and redirect not working

Unanswered
California pilchard posted this in #help-forum
Open in Discord
California pilchardOP
I wanna access to user profile with /@myusername url. But the real route is /user/[username]/page.tsx. So I do this :
// next.config.js
async redirects() {
    return [
      {
        source: '/user/:username/:path*',
        destination: '/@:username/:path*',
        permanent: true,
      },
    ];
  },
  async rewrites() {
    return [
      {
        source: '/:lang/@:username/:path*',
        destination: '/:lang/user/:username/:path*',
      },
    ];
  },

Its working with basic username like loup, bob, .... but when having a point for example loup.one I got an 404. How can I allow everything according to the username regex (same as instgram):
username ~* '^(?!.*\.\.)(?!.*\.$)[^\W][\w.]{2,14}$'::text


Thx !

2 Replies

California pilchardOP
It seems there is an issue with dynamic route containing dot
California pilchardOP
Well the problem was in my middleware :
// previous matcher
matcher: ['/((?!api|_next|_vercel|.*\\..*).*)'],
// new matcher WORKING
matcher: ["/((?!api|static|_next|favicon.ico|robots.txt)(?!.*\\/[^\\/]*\\.[^\\/]{2,4}$).*)"]