Next.js Discord

Discord Forum

how to rewrite in next 14 with .mjs config

Answered
American Chinchilla posted this in #help-forum
Open in Discord
American ChinchillaOP
Hello, I looked at the docs on how to rewrite and it only shows for modules, but what about using .mjs
as below? The rewrite is not working as expected, nothing changes.

/** @type {import('next').NextConfig} */
import withYAML from 'next-yaml';
const nextConfig = {
  async rewrites() {
    return [
      {
        source: '/',
        destination: '/plumbers-in-miami',
      },
    ]
  },
};

export default withYAML(nextConfig);
Answered by joulev
you need to put it inside beforeFiles, by default it's afterFiles
View full answer

52 Replies

American ChinchillaOP
update: im trying to switch to .js instead
hmmm
nothing changed
// @ts-check
const withYAML = require('next-yaml');
/** @type {import('next').NextConfig} */
const nextConfig = {
  async rewrites() {
    return [
      {
        source: '/',
        destination: '/plumbers-in-miami',
      },
    ];
  },
};

module.exports = withYAML(nextConfig);
American ChinchillaOP
Bump ^
alright so it turns out it's not a bug but an intended behaviour
you need to put it inside beforeFiles, by default it's afterFiles
Answer
see more here, they explain it in quite a lot of details: https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites
import withYAML from "next-yaml";

/** @type {import('next').NextConfig} */
const nextConfig = {
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: "/",
          destination: "/plumbers-in-miami",
        },
      ],
    };
  },
};

export default withYAML(nextConfig);
American ChinchillaOP
@joulev thanks, hmm when i do that now on local dev i get page does not exist for "/"
its like it doesnt rewrite or mask the url
hmm wait i think i need a page.tsx with that route /miam right?
i thought i could just change the url let me try this
American ChinchillaOP
hmm my goal is i want to change the url's name
without creating a route etc
as i dont want to redirect the root path to another path as that causes bad seo for google and other problems...
@American Chinchilla hmm my goal is i want to change the url's name
Not sure what you meant by this.
American ChinchillaOP
@joulev like i want the default path that is "localhost:3000" to be changed to "localhost:3000/plumbers-in-miami"
kinda like what .htaccess does
So you want to render /plumbers-in-miami when users navigate to /? With the url still being /?
American ChinchillaOP
yes
almost like a masking
for google seo
Then just create app/plumbers-in-miami/page.tsx and do the rewrites config like above?
American ChinchillaOP
oh i see... so there is no other way then...
its because if i do that
it affects the seo
in a bad way
for too many redirects
Why does it affect the seo?
American ChinchillaOP
because currently the website i was hosting in by go daddy so i had to redirect that domain to vercel
No it’s not a redirect. It’s a rewrite. Google only sees it as /. Heck, google might not even know /plumbers-in-miami exists
American ChinchillaOP
it also affects using third party services
like semrush
i cant see metrics if i use redirects
Once again, it’s not a redirect. It is a rewrite. Nextjs routes / to the plumber page.tsx. This process is completely hidden from the clients visiting the page. Google does not know about the rewrite.
American ChinchillaOP
oh okay
It’s not a redirect where / returns 3xx telling the client to make another request to /plumber-whatever
American ChinchillaOP
but does google still read the url?
after the rewrite
Here / return 200 with the content of the plumber page
@American Chinchilla but does google still read the url?
Google only knows it as /
American ChinchillaOP
i see so it doesnt change anything
thanks for explainning
i guess i wont use rewrites then
::solved
::solved!
!solved
::marked