2

I added a No Match 404 error page to my website, and it works fine when I go to the wrong path on localhost, but after I deploy and go to an invalid path like .../invalidpath I get the default netlify page not found error. Below is my App.js component:

import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Navigation from './components/navigation/Navigation';
import Home from './pages/home/Home';
import Projects from './pages/projects/Projects';
import Contact from './pages/contact/Contact';
import NoMatch from './pages/404page/404Page';

import './App.scss';

function App() {
  return (
    <div className='app'>
      <Navigation />
      <Switch>
        <Route exact path='/' component={Home} />
        <Route path='/projects' component={Projects} />
        <Route path='/contact' component={Contact} />
        <Route path='*' component={NoMatch} />
      </Switch>
    </div>
  );
}

export default App;
2
  • Have you tried not specifying a path for your 404 route? This is the more typical way of defining a 404 route. I don't know if it'll effect netlify however. Commented Mar 14, 2020 at 23:49
  • Yeah I tried it without the path and still wouldn’t work. Commented Mar 14, 2020 at 23:54

1 Answer 1

4

If you've built the app with create-react-app and you're deploying to Netlify, you need to add a _redirects file inside the public directory of your project in order to support client side routing (React Router). It should have this inside:

/*  /index.html  200

Details here. Also, check the official docs from Netlify regarding for Redirects and rewrites

Sign up to request clarification or add additional context in comments.

3 Comments

the website you linked says the file should be called _redirects, however it still doesnt work for me
Try re-deploying the app to Netlify @notacorn that should publish the latest changes.
this was the answer for me stackoverflow.com/a/69901140/8652920

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.