Netlify site name: https://rusmtest.netlify.app/
Issue: Redirection in Next.js middleware doesn’t work as expected for locale handling.
I’m using redirects in middleware with Next.js v14. Here’s a snippet from my next.config.js
:
i18n: {
locales: ['en', 'uk-ua'],
}
In my middleware code:
export default async function (req: NextRequest, ev: NextFetchEvent) {
if (req.nextUrl.locale === 'uk-ua' && req.nextUrl.pathname.startsWith('/opera')) {
req.nextUrl.locale = 'en';
req.nextUrl.pathname = '/about';
return NextResponse.redirect(req.nextUrl, 301);
}
return middleware(req, ev);
}
When I visit the URL http://rusmtest.netlify.app/uk-ua/opera
, it redirects to http://rusmtest.netlify.app/uk-ua/about
. However, in other services, the redirection happens to the /en/
locale, meaning http://rusmtest.netlify.app/about
(without the /en
prefix, as English is the default language).
I tried these alternative redirections:
return NextResponse.redirect('http://rusmtest.netlify.app/en/about', 301);
and
return NextResponse.redirect('http://rusmtest.netlify.app/about', 301);
Neither of these solutions worked, and it seems like Netlify ignores the locale setting.
Did you try Ask Netlify?: Yes, but I couldn’t find a solution.
Is there any way to resolve this?