Astro throws “Invalid internationalization middleware configuration” when you call the middleware() function from astro:i18n with redirectToDefaultLocale: true but prefixDefaultLocale: false. Redirecting / to /en/ only makes sense when the default locale has a prefix; without one, the redirect would loop. Set prefixDefaultLocale: true, or set both options to false.
Key Takeaways
- The error comes from the
middleware()helper used withi18n.routing: 'manual'.redirectToDefaultLocaleneedsprefixDefaultLocale: true.- With no prefix, the default locale already lives at
/, so there is nothing to redirect to.- The same rule applies in
astro.config.mjs, where Astro reports it as a config error instead.- Pick one URL style for the default locale and set both options to match it.
The error
Invalid internationalization middleware configuration.
The option `redirectToDefaultLocale` can be enabled only when `prefixDefaultLocale` is also set to `true`; otherwise, redirects might cause infinite loops. Enable the option `prefixDefaultLocale` to continue to use `redirectToDefaultLocale`, or ensure both are set to `false`.
Why it happens
With i18n.routing: 'manual', Astro turns off its built-in i18n routing and you compose it yourself in src/middleware.ts. The helper accepts the same routing options as the config. This combination fails at request time:
// src/middleware.ts (fails)
import { middleware } from 'astro:i18n';
export const onRequest = middleware({
prefixDefaultLocale: false,
redirectToDefaultLocale: true,
fallbackType: 'redirect',
});
prefixDefaultLocale: false says English pages live at /about/, not /en/about/. redirectToDefaultLocale: true says send / to /en/. Both cannot be true, and the TypeScript types for the helper reject the combination too.
Fix: choose one URL style
If your default locale should have a prefix (/en/, /fr/), set both to true:
// src/middleware.ts
import { middleware } from 'astro:i18n';
import { sequence, defineMiddleware } from 'astro:middleware';
const timing = defineMiddleware(async (context, next) => {
const start = Date.now();
const response = await next();
console.log(`${context.url.pathname} ${Date.now() - start}ms`);
return response;
});
export const onRequest = sequence(
timing,
middleware({
prefixDefaultLocale: true,
redirectToDefaultLocale: true,
fallbackType: 'redirect',
}),
);
If the default locale should live at the root, set both to false:
// src/middleware.ts
import { middleware } from 'astro:i18n';
export const onRequest = middleware({
prefixDefaultLocale: false,
redirectToDefaultLocale: false,
fallbackType: 'redirect',
});
With a prefixed default locale and the redirect on, you need a root src/pages/index.astro; see index page not found.
The config-file version
If you are not using manual routing, the same mistake in astro.config.mjs fails config validation with a similar message about i18n.routing.redirectToDefaultLocale:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
i18n: {
defaultLocale: 'en',
locales: ['en', 'fr'],
routing: {
prefixDefaultLocale: true,
redirectToDefaultLocale: true,
},
},
});
Our Astro i18n routing guide compares the URL styles, the middleware guide explains sequence(), and the Astro config docs cover the config file. The official i18n routing reference lists every option. Most Astro themes ship in one language, so i18n is something you add on top.
FAQ
What does prefixDefaultLocale do in Astro?
When true, the default locale’s pages use a prefix like /en/about/. When false, they live at the root, like /about/, and only other locales get a prefix.
What does redirectToDefaultLocale do?
With prefixDefaultLocale: true, it redirects the site root / to the default locale’s home, such as /en/.
Do I need i18n.routing: ‘manual’ to use the middleware helper?
Yes. The middleware() function from astro:i18n is for manual routing, where Astro’s automatic i18n middleware is disabled and you add it yourself.