Next.js Discord

Discord Forum

How to rewrite root path to /[locale] path in next 13 with app dir?

Unanswered
Isac Paz posted this in #help-forum
Open in Discord
I'm trying to show the index page of my website in the default language pt-BR, so when user access that page the url will be '/', but the [lang]/pt-BR will be render.

I try this:

const nextConfig = nextTranslate({
  async rewrites() {
    return [
       {
          source: '/',
          destination: '/pt-BR',
       },
    ]
  },
});

module.exports = nextConfig;

7 Replies

You want to hide the locale in the url, correct?
For example rewrite /pt-BR/about to /about?
Note that if you followed the i18n example given in Next.js' repo, all pages (including the root page) will be prefixed with the locale.
Actually my structure is it:
the translations are working correctly, but always with the prefix in the url. I don't need to hide all prefixes, just pt-BR. I'd like to leave pt-BR as the default and omit it.
You have to check if the default locale is in the request pathname, and if it's there rewrite the request to a url without the locale.

You can find an example here: https://github.com/milovangudelj/milovangudelj.com/blob/master/middleware.ts#L45-L64