diff --git a/packages/next/lib/load-custom-routes.ts b/packages/next/lib/load-custom-routes.ts index 0879802ca94d9..b044c6592c426 100644 --- a/packages/next/lib/load-custom-routes.ts +++ b/packages/next/lib/load-custom-routes.ts @@ -360,8 +360,8 @@ export default async function loadCustomRoutes( if (config.trailingSlash) { redirects.unshift( { - source: '/:path*/:file.:ext/', - destination: '/:path*/:file.:ext', + source: '/:file((?:[^/]+/)*[^/]+\\.\\w+)/', + destination: '/:file', permanent: true, }, { diff --git a/test/integration/trailing-slashes/test/index.test.js b/test/integration/trailing-slashes/test/index.test.js index 3068d0d2b6d55..24c58e6170fad 100644 --- a/test/integration/trailing-slashes/test/index.test.js +++ b/test/integration/trailing-slashes/test/index.test.js @@ -258,13 +258,13 @@ describe('Trailing slashes', () => { expect.objectContaining({ redirects: expect.arrayContaining([ expect.objectContaining({ - source: '/:path*/:file.:ext/', - destination: '/:path*/:file.:ext', + source: '/:file((?:[^/]+/)*[^/]+\\.\\w+)/', + destination: '/:file', statusCode: 308, }), expect.objectContaining({ - source: '/:path*/:notfile([^/.]+)', - destination: '/:path*/:notfile/', + source: '/:notfile((?:[^/]+/)*[^/\\.]+)', + destination: '/:notfile/', statusCode: 308, }), ]),