0

devs!

I'm trying to build a multilingual website based on Next.js(15.6) Node 22.21.10. Locally I have no issue with routing everything works as expected. Both lang versions are served as expected. (have toggle in header).

Navigation doing its job. site.com gives me correct lang version and if im switching to EN all pages are served correctly in English, however after deploying it to Digital Ocean droplet breaks it(Only front page all other pages are fine).

So my question is, is that a misconfiguration in Next or should I change something in DO droplet?

Sharing my middleware.ts

import createMiddleware from 'next-intl/middleware';

export default createMiddleware({
    locales: ['ru', 'en'] as const,
    defaultLocale: 'ru',
    localePrefix: 'as-needed',
    localeDetection: false,
});

export const config = {
    matcher: ['/((?!_next|.*\\..*).*)'],
};

All my pages are in the app/[locale]/*.tsx (and subfolders for other pages)

I dont have any root page.tsx/layout.tsx

So, my question is what am I missing here?

Please, let me know if I should share more info/files

Thanks!

EDIT:

That happens only with main page. All other pages, categories, single items. e.g. site.com/items works as expected and serves right version

On the server I get 302 redirect on main page

2 Answers 2

0

You can explicitly tell Next that / should go to /ru (or whatever your default locale is):

import { NextResponse } from 'next/server';
import createMiddleware from 'next-intl/middleware';

const intlMiddleware = createMiddleware({
  locales: ['ru', 'en'] as const,
  defaultLocale: 'ru',
  localePrefix: 'as-needed',
  localeDetection: false,
});

export default function middleware(req: Request) {
  const url = new URL(req.url);

  // Handle root path manually
  if (url.pathname === '/') {
    url.pathname = '/ru';
    return NextResponse.redirect(url);
  }

  return intlMiddleware(req);
}

export const config = {
  matcher: ['/((?!_next|.*\\..*).*)'],
};

This ensures that https://example.com/https://example.com/ru always works even in production. (You can redirect to /en if you prefer English default.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply! The issue is that I want to keep main version clean as site.com with no extension, so ru should be default, but english needs to have extension. The code you gave me generates infinite redirect loop. I was trying to modify it, no success achieved. Maybe you can think of an issue on the server form ur expirience? since locally I have no issues
0

Turned out, that middleware is perfectly fine. Deploying stuff to Digital Ocean droplet I've missed to configure Nginx server correctly. On top of its configuration I've had

# Redirect root to default locale
# location = / {
#    return 302 /en;
# }

So just removing that line fixed the issue and site works as expected. So always check server conf facing discrepancies between local and prod.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.