Categories
React Answers

How to fix React-router URLs don’t work when refreshing or writing manually?

Spread the love

To fix React-router URLs don’t work when refreshing or writing manually, we should fix our server config.

For instance, we write

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]
</IfModule>

in the .htaccess file if we’re using Apache.

The file redirects all requests to index.html of the React app so that React Router can be used to render the routes instead of the server.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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