0

I built up a React App using the Facebook create-react-app and everything works fine locally, I also have a hosting plan that uses an Apache server.

Trying to learn more about client vs. server side routing I found this great post, which makes a lot of sense to me. I want to use the Catch-all solution (although, I am using react-router v4, which seems to operate somewhat differently than v3)...

As someone completely new to back-end servers, and no Apache knowledge are there any good guides or suggestions re how to set up a React App on an Apache server (with react-router v4)? (This is my first attempt at setting something up on a server.)

Thanks for any guidance.

vesperae
  • 1,291
  • 2
  • 19
  • 28

2 Answers2

1

Figured this one out on my own, was quite more straight forward than expected. I just ran npm build & placed all my build files at the root. And then edited the .htaccess file to say:

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

So far, everything seems to be working perfectly. Let me know if there's any concerns with this solution. As this was built on top of the create-react-app it also did not requiring to eject the built in webpack config.

Thanks for the advice.

vesperae
  • 1,291
  • 2
  • 19
  • 28
0

I ran into your problem when I came to host my first client-side rendering app with React. To make it even worse, I was using a PHP MVC framework as backend of the API used by the app. API calls which I had to access locally on the same server and enviorment.

This is how I solved my problem:

  1. put the HTML code of build/index.html in the index file of my MVC application
  2. Moved static content (CSS,JSS bundles) reachable for my back-end system. That was in root/build
  3. Edited assets references in the index file (changed in step 1) to point to the new location (changed in step 2)

And it worked perfectly fine!

Now in your case, you can follow the same pattern and put your build/index.html file as if you are running a normal HTML page and move the assets to a reachable location.

Matthew Barbara
  • 3,792
  • 2
  • 21
  • 32