How to use only sub-domain based routing on when using i18

31 views Asked by At

Here is my folder structure-

image

on i18n.ts

import { notFound } from 'next/navigation';
import { getRequestConfig } from 'next-intl/server';

const locales = ['en', 'de'];

export default getRequestConfig(async ({ locale }) => {
    if (!locales.includes(locale as any)) notFound();

    return {
        messages: (await import(`../messages/${locale}.json`)).default
    };
});

then middleware.ts

import createMiddleware from 'next-intl/middleware';

export default createMiddleware({
    locales: ['en', 'de'],
    defaultLocale: 'en',
    domains: [
        {
            domain: 'en.localhost:3001',
            defaultLocale: "en",
            locales: ["en"]
        },
        {
            domain: 'de.localhost:3001',
            defaultLocale: 'de'
        }
    ]
});

export const config = {
    matcher: ['/', '/(de|en)/:path*']
};

So it working and redirecting like-

http://en.localhost:3001/en

It is well documented on - https://next-intl-docs.vercel.app/docs/routing/middleware#domain-based-routing

But I want only sub domain redirect like-

  1. en.localhost:3001
  2. de.localhost:3001

So, here I add this line on createMiddleware-

localePrefix: 'never'

Then it is not working, it is not redirecting to en.localhost:3001

Please help me, anyone!!

0

There are 0 answers