-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Description
Bug report
Describe the bug
Redirect with { locale: false }
isn't working properly.
Context
Before describe the bug, I need to give you some context about what problem I'm trying to solve.
I recently switch my website default locale from pt
to en
. It means that some old URLs I had localized, now would be inverted, for example:
URL example:
Before: /blog/e-o-coronavirus-hein (default lang PT)
After: /pt/blog/e-o-coronavirus-hein (Default lang EN)
The problem
What's is happening now is I can't just setup this redirect via next.config.js
or via vercel.json
(but I'll focus on next config).
The way I would try to solve that is setting up something like this:
module.exports = {
async redirects() {
return [
{
source: "/blog/e-o-coronavirus-hein",
destination: "/pt/blog/e-o-coronavirus-hein",
permanent: true,
},
];
},
};
This won't work and that's comprehensible. I assume when I try to access /blog/e-o-coronavirus-hein
, since my browser is in English, it'll try to resolve the request locale to /en
. But the stranger thing is that the redirect works but it concatenates everything and I'm being redirected to: /en/pt/blog/e-o-coronavirus-hein
.
That's weird but makes sense because I'm embedding the locale in the path.
In the docs it says we can turn off the locale process, that should do the job, right?
module.exports = {
async redirects() {
return [
{
source: "/blog/e-o-coronavirus-hein",
destination: "/pt/blog/e-o-coronavirus-hein",
locale: false,
permanent: true,
},
];
},
};
But now if I try to access /blog/e-o-coronavirus-hein
, instead being redirected to /pt/blog/e-o-coronavirus-hein
, I just reach 404.
I assume that somehow it's thinking that's is a valid route (because I use pages/blog/[slug].js
) and it does not consider the redirect setup.
Another problem (which I can open another issue if you want to) is when I try to redirect from a url which even does not exist from an existing one:
module.exports = {
async redirects() {
return [
{
source: "/2018/08/09/coronavirus",
destination: "/pt/blog/e-o-coronavirus-hein",
locale: false,
permanent: true,
},
];
},
};
In this case I also get 404.
To Reproduce
I prepare a demo site with some deeper explanation how to test this problem but:
- Access: https://next-redirect-bug.vercel.app/
- Check the
Defective redirects
section - Click in both links which should redirect
Expected behavior
As a dev user, I want to be able to setup a redirect from a /blog/some-post
to /<locale>/blog/some-post
.
Screenshots
Not applicable
System information
- OS:
Pop!_OS
- Browser (if applies) [e.g. chrome, safari]:
any
- Version of Next.js: [e.g. 10.0.1]:
10.0.3-canary.3
- Version of Node.js: [e.g. 12.0.0]:
12.19.1
- Deployment: [e.g. next start, next export, Vercel, other platform]:
Vercel
Additional context
- Repository: https://github.com/raulfdm/next-redirect-bug
- Related: redirects i18n locale:false not working as expected #19302
- Ensure i18n custom routes resolves correctly #19766
Update
- This also does not work with
rewrites