Question

I can't reach sub urls on my react app

I deployed my React application as static service. The app works fine for the root path (/), but when I try to access any subpage URL directly (e.g., /move-address-detail, /building-detail), I receive a 404 Not Found error.

I am using React Router for client-side routing, and I have added an .htaccess file in my project’s root directory with the following content:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.html [L]
</IfModule>

How can I resolve the 404 error and make app correctly serve the React app’s subpages?

Thanks in advance


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
December 19, 2024

Heya,

Can I ask why are you not using Routers?Tthese paths are meant to be handled by React Router client-side and not as actual files on the server.

alexdo
Site Moderator
Site Moderator badge
December 19, 2024

Heya, @tumerseyrek

On top of what’s mentioned you can ensure that the .htaccess file is s placed in the correct location whch will be the root of your deployment directory.

Additionally, ensure that the server’s configuration allows .htaccess overrides. Open your Apache configuration file and check that the <Directory> directive contains AllowOverride All

Regards

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.