B
B
Bizki2021-02-16 19:28:51
React
Bizki, 2021-02-16 19:28:51

How to redirect user from non-existent URL?

Hello, I can't figure out how to redirect a user from a non-existent URL. There is a Car component with the /car route, it displays a list of links to the child components of the cars, they have the Car/:name route, let's say I want that if the user enters a non-existent URL in the browser and he gets not "Car / Ford", but " Car/qweqw" then it would redirect to the Car/
PS page and shouldn't it redirect to the main page, because it goes through all the routes, does not find what it needs and should redirect to "/"

<Switch>
        <Route path="/" exact render={() => <h1>Home Page</h1>} />        
          <Route path="/cars/:name" component={CarDetail}></Route>
          <Route path="/cars" component={Cars}/>
          <Redirect to={'/'} />
        </Switch>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SeniorScript, 2021-02-16
@SeniorScript

<Route path='*'>
   <Redirect to='/' />
</Route>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question