How to fix BrowserRouter for React Apps on Apache

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 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Β, you’ll soon notice that there is another issue. Each click to a new route will transform the URL to something like – 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

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.

Improve your React Skills!

Learn about React concepts, helpful libraries or get tips & tricks for deploying your app and many more topics.

I semi regularly post about React. Don't miss out on my future posts! Sign up below to get them delivered directly into your inbox!

34 thoughts on “How to fix BrowserRouter for React Apps on Apache”

  1. After hitting about 10 other “solutions” elsewhere, yours is the one that really worked.
    Thanks for the tip Andreas!!

  2. Great, but is not working for me. I’m trying to deploy OHIF viewer, that is based on react, and my problem is the /viewer route never gets called. I’ve tried using the .htaccess file with de directives you posted, but I keep getting 404. Tried some other directives using VirtualHost and RewriteEngine and the same effect. Maybe you could guide me what can I do in this scenario. Thank you.

    1. Hmm… can you check if the RewriteModule in your Apache is activated? Seems like Apache is not redirecting your request to the index.html file.

  3. Hi, thanks for sharing… I’m facing similar issue with tomcat (PSB

    And I noticed that tomcat don’t load the component of “/” route automatically when page is loaded, If I add links to these routes and press on them then components are loaded.

    Following this link:, tried implementing the RewriteUrl on tomcat 7, but it didn’t help!

    Out of ideas, any hints?

  4. I wanted to thank you dear.
    This saved me while I was struggling for almost 3 hours in midnight.
    Thqnk you and hope see your thoughts again πŸ™‚

  5. Hey Andreas,

    First thanks for the post, very helpful!!!

    I was wondering how can I make it work with nested routes like this:

    It works fine with this:

    But it doesn’t works with the nested ones

    1. Hi Jordan!
      Sorry for the late reply – can you provide more information about the issue?
      Have a look at your browser console when you’re trying to access and tell me what you can find there.
      It’s probably “just” a configuration issue.

  6. Thank you for your valuable tips. Now I have been able to solve 404 issue by adding .htaccess file and working as a charm.

  7. Esta soluciΓ³n no me ayuda, al implementar los cambios me sigue mostrando una pantalla en blanco πŸ™

    1. This solution does not help me, when implementing the changes it still shows me a blank screen πŸ™

Leave a Reply

Your email address will not be published. Required fields are marked *