Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-router auto redirect any route to root "/" except for "/main"

I used react-router, even tried <Redirect .. />, I can't redirect to the /, the main problem is the / is out of the other routes, so I need to use <Redirect from="*" to="" />, it seems to not work.

then I also need to prevent /main redirecting to /, I put the <Redirect .. > after <Route component={MainView} path="main">

another requirement, how make the localhost:3000 to localhost:3000/ to use AppIndex IndexRoute

please check code below:

import MainView from 'components/MainView';
import ThemeIndex from 'components/ThemeIndex';
import AppIndex from 'components/ThemeIndex';
import Shop from 'components/Shop';
import App from 'components/App';

export default (
    <Route component={App} path="/">
      <IndexRoute component={AppIndex}></IndexRoute>
      <Route component={MainView} path="main">
        <IndexRoute component={ThemeIndex}></IndexRoute>
      </Route>
      <Redirect from="*" to="" />
    </Route>
);

have any idea?

like image 486
Seven Lee Avatar asked Jun 16 '16 03:06

Seven Lee


1 Answers

Instead of leaving to an empty string, you have to specify the path, which means <Redirect from="*" to="/" /> in this case.

To make the localhost:3000 to localhost:3000/, you don't have to do anything, your current code already get the job done. If you go to any website's root URL, Say http://stackoverflow.com, and try type location.pathname in console, you'll get "/"

Please ask me if you have any other problem.

like image 181
David Guan Avatar answered Nov 10 '22 00:11

David Guan