Support Forums

Next.js Rewrites not working

I don’t think creating an issue on something that’s working ‘as expected’ is a good idea.

However, about the 301 redirect, it’s because the redirect is not forced, 301!. Since index.html exists, it’s not getting redirected. Either remove the html file or change the rule to 301!.

Furthermore, I think it’s working in Netlify CLI because of the Next.JS server that’s working locally. That might be able to handle the rendering of the page. The 200 rewrite’s working fine, it’s just that because the function doesn’t have the data for /index.html, it’s unable to generate the HTML for that page. It needs to see /home in the address bar for it to work.

Holding off with creating an issue for the time being is what I would advice.

1 Like

@hrishikesh Okay, will hold that.

I don’t still understand how a third party URL can work but local link cannot work.


/     https://somesite.com/home   200!

In the above case it works, netlify just rewrites to that page. So here, netlify don’t need to think if home exist or not. It just rewrites what https://somesite.com/home returns.

Then in case of this:

/     /home   200!

netlify needed a static /home page? Why? Why can’t netlify just go to that URL, ie https://samesite.com/home in the backend and return what it returns? Why need to check the existence?

That’s my question and I’m sure its some kind of bug.

Let me explain what’s happening once again.

As I said above, redirects are working just fine. So, your /index.html is getting rewritten to /home. However, the difference in the above 2 cases that you mentioned is that, the former has a page with a ready-made output that Netlify can “just display”. The latter one is a page that doesn’t really exist and needs to be rendered on the fly. The issue here: Netlify's rewrite rule doesn't work with fallback pages · Issue #455 · netlify/netlify-plugin-nextjs · GitHub can explain the behaviour in more detail. To summarise, a 200 rule is applied only once. Since your /home route needs another 200 rule to be actually able to reach the function endpoint and generate a page, it never works.


So I think either Netlify should pre-render every static pages so /home is already live. Then just redirect to that page just as any third party.

If not, netlify dev should show the error that its not supported. So we don’t spend days making a feature and only came to know it won’t work.

Well, Netlify does have a pre-rendering service but that’s only to serve the bots and the feature needs to be enabled. If Netlify pre-renders each (Next.JS) website, that might be a huge load on the CDN in its current stage.

This is a very specific use case. Most users have their homepage at /index.html, considering Netlify anyhow wants to have an index.html to serve or it results in a 404. So we’re probably not prepared for such unique use cases. With that being said, I think I gave you a possible explanation of why it’s working with CLI. It might be the Next.JS local development server that is returning the required page to the CLI server. The CLI server is not rendering anything, it’s just displaying what’s being rendered by the development server of the application.

We can add this as a feature request to show an error or a warning that it might not work. But then, as I said, since the use case is very specific, this might not make into a release.

If you still wish to wait for a final confirmation, I could revert after the second internal discussion that I had started a few messages ago. But yes, chances are it might not work.