Next.js Discord

Discord Forum

Can I add a subdomain in Next?

Unanswered
Quokka posted this in #help-forum
Open in Discord
Let's say I have the route /[locale]/admin; I want for it to be on admin.localhost:3000/[locale] if possible.

E.g. if I have /[locale]/admin/dashboard, it will be on admin.localhost:3000/[locale]/dashboard.

Is there any way to achieve this? I'm also confused by [rewrite](https://nextjs.org/docs/pages/api-reference/config/next-config-js/rewrites) and whether or not I can apply it here

3 Replies

I tried adding this to my next.config.ts:

import { NextConfig } from 'next';
import createNextIntlPlugin from 'next-intl/plugin';

const nextConfig: NextConfig = {
    async rewrites() {
        return {
            beforeFiles: [
                {
                    source: '/:locale/admin/:path*',
                    destination: `${process.env.NEXT_ADMIN_URL}/:locale/:path*`,
                },
            ],
        };
    },
};

const withNextIntl = createNextIntlPlugin();
export default withNextIntl(nextConfig);


but going to http://localhost:3000/en/admin does nothing. (NEXT_ADMIN_URL='http://admin.localhost:3000')
I'm using the standard structure used by next-intl. [Here are the docs](https://next-intl.dev/docs/getting-started/app-router/with-i18n-routing)
So my middleware looks like

import createMiddleware from 'next-intl/middleware';
import {routing} from '@/i18n/routing';
 
export default createMiddleware(routing);
 
export const config = {
  // Match all pathnames except for
  // - … if they start with `/api`, `/trpc`, `/_next` or `/_vercel`
  // - … the ones containing a dot (e.g. `favicon.ico`)
  matcher: '/((?!api|trpc|_next|_vercel|.*\\..*).*)'
};