anon
anon

Reputation: 634

React Router - Redirect to route if default language is in URL

I have this route setup

<IntlProvider locale={locale} messages={messages[finalLang]}>
  <BrowserRouter>
    <Route>{props => <Core {...props} routes={routes} locale={finalLang}/>}</Route>
  </BrowserRouter>
</IntlProvider>

Everything works as it should, I have routes such as

/home,
/en/home

However, if the user enters /fr/home, I would like it to redirect to /home since FR is the default language. Been trying to figure this out, and not sure yet

Upvotes: 2

Views: 1901

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281686

You can simply create a Route config that Redirects to /home like

<Switch>
   <Route exact path="/home" component={Home}/>
   <Redirect exact from="/fr/home" to="/home" />
   <Route path="/:lang/home" component={SomeComp} />
</Switch>

Upvotes: 3

Related Questions