Next.js Discord

Discord Forum

Rewrite not working

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Avatar
Asiatic LionOP
Hello all,

I'm not sure I understand the rewrite configuration correctly.

I have setup two minimal projects using the documentation.
I start project 1 on port 3000 and project 2 on port 3001.

What I expect to see is by navigating to http://localhost:3000/about I would see http://localhost:3001/about but keeping the url of port 3000.

In my next.config.js I have the following:
module.exports = {
  async rewrites() {
    return [
      {
        source: '/about',
        destination: 'http://localhost:3001/about',
      },
    ]
  },
}



I have tested the following setup:
Project 1 with an about page => it shows the about page from this project
Project 1 without an about page => it returns blank page and loads of 404 for javascript files

I also tried with beforeFiles and afterFiles configuration but nothing works.

Any ideas?
Thanks!
Answered by Asiatic Lion
Another finding:
Project 2 on port 3001 was running using npm run dev
I have tried running it using npm run start
The rewrite now works
View full answer

5 Replies

Avatar
Yacare Caiman
the destination is the path of your application
Avatar
Asiatic LionOP
Hi Huy,
Thanks.
I'm not sure what you mean by that?
I have followed rewrite to external URL section in the doc: https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites#rewriting-to-an-external-url
My destination path is the other project that runs on http://localhost:3001
Avatar
Yacare Caiman
It looks like you are using pages router
Avatar
Asiatic LionOP
I just did a test with destination https://www.google.com/
This works but I don't understand why it doesn't work with another NextJS project
Avatar
Asiatic LionOP
Another finding:
Project 2 on port 3001 was running using npm run dev
I have tried running it using npm run start
The rewrite now works
Answer