user7734639
user7734639

Reputation: 31

React-router: Redirect as default route to static page in public

I want that the default route - in case that no route in the router match to the url will redirect the user to static html page that in the public folder in my project. the router knows the static html page - if I type the address I get the page, but its doesn't work with the redirect.

this is how I try to write it:

<Switch>
    <Route exact path="/home" component={home} />
    <Route exact path="/noPermission" component={handleErrorRoute} />
    <Route render={() => <Redirect to={'/404.html'} />} onEnter={reload} />
</Switch>

I tried also:

   <Switch>
        <Route exact path="/home" component={home} />
        <Route exact path="/noPermission" component={handleErrorRoute} />
        <Redirect to={'/404.html'} />
    </Switch>

Upvotes: 1

Views: 489

Answers (1)

Joshua Oweipadei
Joshua Oweipadei

Reputation: 167

You first have to create a component that serves your 404 page

Do this;

import NotFound from '<your_file_path>'

<Switch>
    <Route exact path="/home" component={home} />
    <Route exact path="/noPermission" component={handleErrorRoute} />
    <Route exact path="/not-found" component={NotFound} />
    <Redirect from="/" to="/not-found" />
</Switch>

Upvotes: 1

Related Questions