1

My React application is hosted at www.mywebsite.com/myreact

When I defined my routes, I've done something like this:

<Router history={history}>
    <Route path="/" component={App} />
        <Route path="login" component={Login} />
    </Route>
</Router>

Although, it keeps complaining that /myreact is not defined in the route.

When I define it, that way:

<Route path="/myreact" component={App} />

and I try to access the URL www.mywebsite.com/myreact/login, the browser throw a 404 error.

But I can access www.mywebsite.com/myreact properly though.

What should I do?

Thanks.

3
  • did you try adding myreact to the login route as well? path="myreact/login" Commented Apr 29, 2016 at 21:07
  • Just tried, it doesn't work... Commented Apr 29, 2016 at 21:09
  • try using browserHistory from react-router and your routes like path='/' & path='/login' Commented Apr 29, 2016 at 21:14

1 Answer 1

2

You should be able to create a custom history object with a different basename for your apps 'root' url. with the useRouterHistory enhancement when creating the history object

https://github.com/reactjs/react-router/blob/master/docs/API.md#userouterhistorycreatehistory

some relavent comments in this github issue thread

import { createHistory } from 'history';
import { useRouterHistory } from 'react-router';

const browserHistory = useRouterHistory(createHistory)({
    basename: '/myreact'
});

your routes files should now work using browserHistory

<Router history={browserHistory}>
    <Route path="/" component={App} />
        <Route path="login" component={Login} />
    </Route>
</Router>
Sign up to request clarification or add additional context in comments.

2 Comments

This work perfectly if I create a link to access the login page from the root page. Although if I try to access /myreact/login directly entering the URL, that's when I get the 404 error. Any suggestion?
this is an inherent trait of the browserHistory type of history. if you go to any route manually before the router is attached, you must make a server route and allow your client-side js take over. github.com/reactjs/react-router/blob/master/docs/guides/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.