is it possible to rename routes without renaming the folders?
Answered
American black bear posted this in #help-forum
American black bearOP
Basically I have a folder called about and inside I have the page.tsx file (about/page.tsx). I was wondering if it is possible to rename the path to
so, instead Next.js renders /about, it should actually renders /sobre.
sobre.so, instead Next.js renders /about, it should actually renders /sobre.
Answered by B33fb0n3
you can see the file structure here: https://codesandbox.io/p/devbox/qvlvkx
30 Replies
@American black bear Basically I have a folder called about and inside I have the page.tsx file (about/page.tsx). I was wondering if it is possible to rename the path to `sobre`.
so, instead Next.js renders /about, it should actually renders /sobre.
yes, go into your
Then whenever you visit /about it actually renders /sobre
next.config.js and add a rewrite:module.exports = {
async rewrites() {
return [
{
source: '/about',
destination: '/sobre',
},
]
},
}Then whenever you visit /about it actually renders /sobre
American black bearOP
yeah I tried this but nothing happens
what happens when you visit
/about?American black bearOP
it is redirecting me to /
@American black bear it is redirecting me to /
Add
That works fine as you can see in this repro: https://codesandbox.io/p/devbox/qvlvkx
beforeFiles to it. Like this:{
async rewrites() {
return {
beforeFiles: [
{
source: "/about",
destination: "/sobre",
},
],
};
},
}That works fine as you can see in this repro: https://codesandbox.io/p/devbox/qvlvkx
@B33fb0n3 Add ``beforeFiles`` to it. Like this:
tsx
{
async rewrites() {
return {
beforeFiles: [
{
source: "/about",
destination: "/sobre",
},
],
};
},
}
That works fine as you can see in this repro: https://codesandbox.io/p/devbox/qvlvkx
American black bearOP
it makes sense because you have sobre/page.tsx and about/page.tsx
my question is: is it possible to make /about/page.tsx renders /sobre without having a folder called sobre?
@American black bear my question is: is it possible to make /about/page.tsx renders /sobre without having a folder called sobre?
no, how does nextjs then know what to render?
American black bearOP
I thought the next.config was going to replace the route or something like that
@American black bear I thought the next.config was going to replace the route or something like that
yea you are right, but that works only for the
As you can see: there is no folder, but
source path. Try to visit /notexists here: https://codesandbox.io/p/devbox/qvlvkxAs you can see: there is no folder, but
sobre will still be rendered@B33fb0n3 yea you are right, but that works only for the source path. Try to visit /notexists here: https://codesandbox.io/p/devbox/qvlvkx
As you can see: there is no folder, but sobre will still be rendered
American black bearOP
in your example if I go to /about page I can see the content of the sobre page but the route is still /about
however this is not what I was looking for... I thought Next.js could make it but maybe it is not possible
I also thought about the metadata, somehow add a prop in there and the magic happens
however this is not what I was looking for... I thought Next.js could make it but maybe it is not possible
I also thought about the metadata, somehow add a prop in there and the magic happens
anyways, thank you 🙂
@American black bear in your example if I go to /about page I can see the content of the sobre page but the route is still /about
however this is not what I was looking for... I thought Next.js could make it but maybe it is not possible
I also thought about the metadata, somehow add a prop in there and the magic happens
as said you can see, you can do that only for the
source path and not for the page that will be rendered. Visit notexists and you can see the result@B33fb0n3 as said you can see, you can do that only for the `source` path and not for the page that will be rendered. Visit notexists and you can see the result
American black bearOP
yeah but it should be the opposite, in my case sobre does not exists, but about exists... so /about should be /sobre
@American black bear yeah but it should be the opposite, in my case sobre does not exists, but about exists... so /about should be /sobre
Which file (which page.tsx) will then be rendered?
American black bearOP
I think you're not following. from what I can see, it’s not possible to achieve what I want
let me try again: I have an about page (/about/page.tsx), but I don’t have a sobre/page.tsx.
since I have multiple brands, I’d like to keep all the files in english because our team is international. however, the routes don’t make sense to remain in english if the brand is only available in Argentina, for example
so, with the about folder containing the page.tsx file, I’d like the route to be /sobre for the brand in Argentina. If the brand is in Brazil, for example, I’d like the route to be /sobre-nos, and so on...
In summary, I have N websites for N brands, which is why I need different names for the directories and their respective paths
let me try again: I have an about page (/about/page.tsx), but I don’t have a sobre/page.tsx.
since I have multiple brands, I’d like to keep all the files in english because our team is international. however, the routes don’t make sense to remain in english if the brand is only available in Argentina, for example
so, with the about folder containing the page.tsx file, I’d like the route to be /sobre for the brand in Argentina. If the brand is in Brazil, for example, I’d like the route to be /sobre-nos, and so on...
In summary, I have N websites for N brands, which is why I need different names for the directories and their respective paths
@American black bear I think you're not following. from what I can see, it’s not possible to achieve what I want
let me try again: I have an about page (/about/page.tsx), but I don’t have a sobre/page.tsx.
since I have multiple brands, I’d like to keep all the files in english because our team is international. however, the routes don’t make sense to remain in english if the brand is only available in Argentina, for example
so, with the about folder containing the page.tsx file, I’d like the route to be /sobre for the brand in Argentina. If the brand is in Brazil, for example, I’d like the route to be /sobre-nos, and so on...
In summary, I have N websites for N brands, which is why I need different names for the directories and their respective paths
But then everything works.
As we saw in my previous examples the „source“ can be anything and does not need to have any files. So in your case it can be the Argentina version (source „sobre-nos“). That’s what the user visits.
For your team they create a folder like „about/page.tsx“ and add the mapping to the rewrites file (destination: about)
Like that the user visits /sobre-nos and sees the content of /about (without noticing it, as the url is still /sobre-nos). However your team (who has access to the files) can read the English version of it and everything is great 🙂
As we saw in my previous examples the „source“ can be anything and does not need to have any files. So in your case it can be the Argentina version (source „sobre-nos“). That’s what the user visits.
For your team they create a folder like „about/page.tsx“ and add the mapping to the rewrites file (destination: about)
Like that the user visits /sobre-nos and sees the content of /about (without noticing it, as the url is still /sobre-nos). However your team (who has access to the files) can read the English version of it and everything is great 🙂
@B33fb0n3 But then everything works.
As we saw in my previous examples the „source“ can be anything and does not need to have any files. So in your case it can be the Argentina version (source „sobre-nos“). That’s what the user visits.
For your team they create a folder like „about/page.tsx“ and add the mapping to the rewrites file (destination: about)
Like that the user visits /sobre-nos and sees the content of /about (without noticing it, as the url is still /sobre-nos). However your team (who has access to the files) can read the English version of it and everything is great 🙂
American black bearOP
yes, but in your example you have both folders and files (about and sobre).
and then using next.config you redirect them from about to sobre. if I delete the sobre folder letting only the about and them rewrite the about with sobre, will I have the same result?
and then using next.config you redirect them from about to sobre. if I delete the sobre folder letting only the about and them rewrite the about with sobre, will I have the same result?
I just want to rename the route without renaming the folder and without creating a new route to rewrite the source (about) with the destination (sobre)
Dwarf Hotot
Remove the sobre folder, keep the about folder then rewrite/about to /sobre then it will work
@American black bear yes, but in your example you have both folders and files (about and sobre).
and then using next.config you redirect them from about to sobre. if I delete the sobre folder letting only the about and them rewrite the about with sobre, will I have the same result?
No I don't have both files. My example in files:
next.config.js:
File structure:
As you can see: in the folder structure is perfect english and the external user can just visit the url
Try yourself:
https://qvlvkx-3000.csb.app/
1. Visit main page
2. Click 'Go to sobre-nor'
3. See that you are on the url with
next.config.js:
{
async rewrites() {
return {
beforeFiles: [
{
source: "/sobre-nor", // external url
destination: "/about",
},
],
};
},
}File structure:
/app/about/page.tsxAs you can see: in the folder structure is perfect english and the external user can just visit the url
sobre-nor 👍Try yourself:
https://qvlvkx-3000.csb.app/
1. Visit main page
2. Click 'Go to sobre-nor'
3. See that you are on the url with
/sobre-nor and you can see the content of /about@Dwarf Hotot Remove the sobre folder, keep the about folder then rewrite/about to /sobre then it will work
no it won't as the folder structure for
/sobre does not exists and also shouldn't exists@American black bear solved?
American black bearOP
apparently not
/sobre
oh, wait... it is sobre-nor
ok, and where is the code for this? I want to check the file structure
@American black bear ok, and where is the code for this? I want to check the file structure
you can see the file structure here: https://codesandbox.io/p/devbox/qvlvkx
Answer
American black bearOP
yup, that's it 🌟