Create-react-app: Document react router basename for relative builds

Created on 22 Jun 2017  路  20Comments  路  Source: facebook/create-react-app

Hi ,

I have created a demo project and deployed to Apache server but React Routes are not working. When i click on product it should go to "/product" but here it shows object not found. Could you please give permanent solution for this.

up for grabs! starter question documentation

All 20 comments

Are you using React Router?

If so, I'd check the configuration. Also you'll need to catch all your path requests to go to index.html so the router can take over.

Are you able to share your code?

Have you done https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#serving-apps-with-client-side-routing ?
tldr creating .htaccess file beside index.html with this content usually do it to me.

    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [QSA,L]

I'll go ahead and close this because we have a section in our documentation which explains how to do this.
@viankakrisna has been kind enough to copy this section for you here. 馃槃

Hi ,
I tried with that also . default page is showing . when we route to other pages it shows "object not found"

@jsk548 sadly, I cannot help much with that information.

  1. Do you have mod_rewrite extension enabled?
  2. What's the actual response that is coming from the server? (a screenshot of dev tools network tab would be sufficient) Is it 200 or 500?
  3. Can you share the URL where the app deployed?
  4. Do you have access to support for maintaining the server?
  5. Can you configure the Apache vhost file?

Further more, here is what i found.

  1. i have deployed as - demo1.gd.com/cliq - (which serves the index)
  2. but i have fouind that when i click on products it should take me to demo1.gd.com/cliq/products instead it takes me to demo1.gd.com/products
  3. it is missing the cliq in the url - demo1.gd.com/cliq whch is the host

It means that you need to set homepage field in the package.json https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#building-for-relative-paths

@viankakrisna samething happening. i added "homepage": "demo1.gd.com/cliq" in package.json no change from previous build. it takes me to demo1.gd.com/products

you need to update your link like this:

<Link to={`${process.env.PUBLIC_URL}/products`}>Products</Link>

@gaearon @Timer I think it's missing from docs? Also, do

<head>
  <base href="%PUBLIC_URL%">
  ...
</head>

in index.html will solve it?

@jsk548 btw, I cannot access demo1.gd.com, is it a virtualhost?

We're hesitant to put the public URL fix in the docs because it might be changing in the future. The HTML base tag looks promising. What kind of browser support does that have?

It's an old spec. So it's pretty well supported. It has gotcha though https://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag
mainly behavior of <a href="#blabla"> and crawlers are not really good with base tag enabled html

I just found out that react router v4 supports basename on BrowserRouter though https://reacttraining.com/react-router/web/api/BrowserRouter/basename-string

Oh sweet we should probably suggest the basename feature in react-router then.

Anybody want to send a doc PR?

Let's open this because I think it's really important

Is there a solution guys?
I tried above solution you mentioned ${process.env.PUBLIC_URL}/products}>Products. But the thing process.env.PUBLIC_URL is always empty.

If you are using react router v4, just use the basename feature linked above. If not, can you share a reproducible example? It's easier to work with code rather than guessing what is wrong.

Hi @viankakrisna ,
basename is working in localhost but not in apache server.
URL http://demo01.gendevs.com/mypro is showing home page.
but when i route to http://demo01.gendevs.com/mypro/contactus it shows 404 Object not found
https://www.dropbox.com/s/owax5grpvchg8b7/example_router4.zip?dl=0

aaah, you are using apache tomcat, the guide is specifically for Apache HTTP Server with support for .htaccess. For Apache Tomcat, I think this SO answer will help https://stackoverflow.com/a/41249464/4878474

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stopachka picture stopachka  路  3Comments

alleroux picture alleroux  路  3Comments

jnachtigall picture jnachtigall  路  3Comments

dualcnhq picture dualcnhq  路  3Comments

DaveLindberg picture DaveLindberg  路  3Comments