DEV Community

Cover image for Page Not Found Error on Netlify Reactjs React Router solved
Rajesh Royal
Rajesh Royal

Posted on • Originally published at on

Page Not Found Error on Netlify Reactjs React Router solved

So you made a Single Page Application and It’s working great on localhost npm or yarn server. And now you made a build of this site and deploy it to the Netlify.

nooo… I got an error.

Page Not Found

Looks like you’ve followed a broken link or entered a URL that doesn’t exist on this site.

Many might run into the following error after deploying a single page React site using React Router to Netlify.

netlify page not found error react router after deploy
Netlify page not found error react-router after deploy

How did the error occur?

React Router handles routing on the client-side (browser) so when you visit the non-root page (e.g., Netlify (server-side) does not know how to handle the route.

(As your routes are set up in the root level).

And the error occurring on Netlify when you go to https://<netlify domain>/route directly.

netlify site not found error react router
Page Not Found Demo Netlify

How can we fix it?

Netlify offers a special file, _redirects, which you can add to your codebase and let Netlify handle the URL that is not handled on the client-side.

Netlify documentation has a section, History Pushstate and Single Page Apps, which shows you how to redirect to the root of your SPA URL (but doesn’t mention React Router, as it applies to other client-side frameworks/libraries).

So to fix the issue, we need to create a file named _redirects to the root of our site [public folder of App] with the following content.

/* /index.html 200

View file on github

redirects file content netlify page not found spa error solved
redirects file content

Here is the working example site: React Todo App with firebase Authentication.

solved netlify error video
Netlify successfully redirected to the SPA routes

For more ways & restrictions for redirect, check out the official Netlify documentation.


The post Page Not Found Error on Netlify Reactjs React Router solved appeared first on Rajesh Royal.

Top comments (72)

mellim profile image
Mel Lim • Edited

Thanks for this, Rajesh! You definitely put me on the right track. I found the _redirects file didn't seem to like working with my netlify.toml, which I was already using to redirect API calls to my backend. I found this sample code to add to my netlify.toml file in the Netlify documentation:

[COMMENT] The following redirect is intended for use with most SPAs that handle routing internally.

from = "/*"
to = "/index.html"
status = 200

Link to documentation here:

Tip: scroll through the sample file that is provided or use find on your browser as it is kind of lost in a sea of other sample code.

rajeshroyal profile image
Rajesh Royal

Thanks @mellim
This detailed comment is definitely going to help somebody.

humblecodes profile image

this did work for me when i deployed it on Netlify platform, but failed to work when deployed the same static site on Render platform. Can you explain, how to solve the issue while deploying on render platform

Thread Thread
rajeshroyal profile image
Rajesh Royal

I never worked with this platform before.

imehusoro profile image
Imeh Usoro

This was my solution. Thanks a lot, Mel!

ariiie_arie profile image

I tried this and still did not work

rajeshroyal profile image
Rajesh Royal

Maybe you missed any config or having some other issue with project. please provide more information so people from comments can also help. Thanks.

anabeatrizzz profile image
Ana Beatriz

For me too.

felipekafuri profile image
Felipe Ramos Kafuri

Thanks thats saved me haha

rajeshroyal profile image
Rajesh Royal

you're welcome 😀

codeclassifiers profile image

Thank you so much for this fix. This is a really useful tip.

dhruvindev profile image
Dhruvin • Edited

very helpful

animeshsinghweb profile image
Animesh Singh

Thanks! Worked awesome.
I had missed the note to add _redirects to public/ folder. Fixed and good!

zaqar profile image

This is very helpful, thank you!

jonatasoc profile image
Jonatas de Oliveira Coêlho

Hey, Rajesh! You helped me a lot! Thank you very much!

melindacardenas profile image
Melinda Cardenas

this worked for me! thank you :-)

roxanaflorea profile image
Roxana F

You are a life saver!!! Thanks from the bottom of my heart <3

rajeshroyal profile image
Rajesh Royal

Your welcome sir 😉

matcs06 profile image
Mateus Costa Silva

You saved me!! Thank you so much!!!

javila35 profile image
Joe Avila

You are doing god's work

rajeshroyal profile image
Rajesh Royal

heheheh 😄

hecthorl profile image
hector vargas

thanks a lot man

kmanadkat profile image
Krupesh Anadkat

Thanks man :)

rajeshroyal profile image
Rajesh Royal

you're welcome 😀

mehmetcanaygun profile image

Thank you! That was what I needed.