Web development basic and advance tutorial, php basic tutorial, laravel basic tutorial, React Native tutorial

Monday, March 20, 2023

How to fix BrowserRouter for React Apps on Apache

0 comments

 While learning React, most people develop and test their apps locally. I mean … they’re just demo apps for learning purposes right? Nobody wants to see stuff like that, so why even think of deploying the apps somewhere?

But then comes the day where you try to deploy a React App to a web server, so you could show it to the people out there: “Look! I made this!”. And that’s how it was for me too.

Deploying a static React bundle to an Apache web space

I already had a web space, so it was obvious for me to put it up there. Because I used create-react-app,  all I had to do was running npm run build and copy the contents of my app’s build folder to said web space.

YEAH! After all that hard work I just deployed my first React app!

I started clicking through several routes and everything seemed fine, until I refreshed the page or tried to access a route directly. I got a 404 error.

“But it works like a charm locally – why doesn’t it on my web space?”

Does that sound familiar to you?

Why is BrowserRouter not working?

Some research brought up several solutions. One of them:

“Use HashRouter instead of BrowserRouter”

Okay that would be an easy one. But since I wanted to have nice and clean URLs without the hash #, that was no option. So let’s dig deeper and have a look at the reason for the error.

When you’re visiting a route of your app directly, e.g. via https://myapp.com/route/123 Apache tries to map that URL to a file in the public folder. In this case it looks for /route/123.html which obviously doesn’t exist – therefore the 404 error.

To avoid that, we have to tell Apache to redirect all requests to our index.html so our app can perform some routing magic for that URL.

Fixing the app’s routing

Now here’s how to finally fix the routing. To tell Apache to redirect requests to index.html where our app lives, we have to modify the .htaccess file. If there is no such file in your app’s folder yet, just create it.

Then be sure that you put in those 4 lines that will magically make your routing work.

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

After we put that .htaccess file into the same directory as the index.html, Apache will redirect each new request directly to your app.

Bonus: Deploying the React app to a sub directory

If you’re deploying your app into a sub directory, so it’s accessible e.g. via https://myapp.com/the-app, you’ll soon notice that there is another issue. Each click to a new route will transform the URL to something like https://myapp.com/route-abc – which will break again after a reload. But there is a simple fix for that:

BrowserRouter has a prop called basename where you can specify your sub-directory path:

<BrowserRouter basename="/the-app">

From now on, each Route like /contacts will result in an URL like http://myapp.com/the-app/contacts.

Wrapping up

Today you learned how to fix routing with BrowserRouter for React Apps deployed to Apache. You learned, why you even get the 404 error in the beginning, and that you can overcome that error by redirecting all requests back to index.html.

The next time you’re facing routing issues with a React App on Apache, head back to this article and try putting the redirect script to the .htaccess file.

Do you know more issues with hosting a static React app on Apache? Leave a comment at the bottom.

No comments:

Post a Comment