Home
Support Forums

Next.js Rewrites not working

I have created a _redirects file in the root of next.js and after building it, I can see it along with the other next.js redirects, but the rewrite I wrote is not working.

Here is what I wanted :- Whenever a user visits the homepage ie / I need to serve /home path as a rewrite proxy. All other path stay same. No changes. This is what I wrote in the _redirects file.

/ /home 200

After the build, I can see the _redirects file in the out folder. But / returns 404.

Can you confirm is this the correct method. Also I have to pass params and netlify docs says it will do automatically.

I did not put /* because I need other subpaths exactly. Only the home/index page needs to be rewritten.

Netlify Staff can see the Project which is in my recent projects starts with “Su”

There are 2 things you could try:

Add an index.html OR change the rule to / /home 200!.

Let us know how it goes.

Thanks @hrishikesh for the reply.

its a next.js app, so not sure how valid is adding an HTML file, Tried ! but the result is same.

tried index.html, but no luck.

hi there, can you confirm that your redirects are being processed?

there is also more debugging info here:

Hello, Yes, I can see it says 10 rules processed as well as the download folder contains the file which includes my rule at top in the first line.

Hi @surjithctly,

Could you try changing the rule to /index.html /home 200!? That should work.

It still doesn’t work.

Now it returns a blank page, I guess its because my index.html is blank.

I have added index.html in the /public folder. Hope that’s fine.

Also I don’t see a function log when I visit / but /home still works as expected. only the rewrite has the issue.

I checked further and I’m having a feeling that this might not be fully possible without a 301 (or 302). The reason is, you don’t have a static HTML file for the /home route. It’s possible that NextJS is handling the rendering of the page based on functions. This might be based on the path string of the URL. When using a 200 redirect, the path in the URL stays /, which is not helping the function.

So how to fix this? if I add 301 it will become a redirect instead of rewrite right?

Yes, it will be a permanent redirect and users visiting / will automatically be moved to /home and they’ll also see the Url as /home.

I want the rewrite only. Is this something Netlify Team can fix? If so how to report it?

I’m not sure if this can be fixed as it’s not something that’s broken. Netlify needs content in /index.html to show as the homepage and the redirects are working as expected. Changing this behaviour is probably not possible, however I’ll try to get more info on this and let you know if there’s anything else that you can do.

However, I think a better way out would be to try to match the traditional web-development patterns and get your home page to be at / instead of /home and then using redirects and proxies to get it to work. It’s adding an unnecessary step according to my opinion. So if it’s a small change that’s possible for you, that would be a good way to go.

2 Likes

Hi @surjithctly,

Upon internal discussion, I can only confirm what I’ve said above. You have 2 options, one that I listed above, 2nd would be to create a static HTMl version of the /home. If both of these options are not good enough, unfortunately your application cannot work as you intend it to, at least not on Netlify.

But it does work with netlify dev without any issues. It automatically rewrites without any problem.

Can you speak with the product team why is that not working only on the server? I just want to make sure before I give up.

If it doesn’t support, netlify cli should also give error, so we can better mimic the environment…

image

Sure thing, I’ll recheck this.

btw, I can also confirm simple redirection also not working. can see on the latest build.

/ /home 301

This doesn’t redirect anywhere. ofcourse it works on netlify dev as well.

Hey @surjithctly ,

You might wish to file and issue on Issues · netlify/cli · GitHub if Netlify dev is performing as you might expect (I had a look and found nothing about this issue already posted.)

You might also find Support next.config.js redirects, rewrites, headers · Issue #150 · netlify/netlify-plugin-nextjs · GitHub a topic of interest.

1 Like

Thanks @coelmay. I will create an issue.

1 Like