I suspect this is not a code issue as the issue only occurs live and there are no console errors or anything else to indicate otherwise. If code examples are needed, I will share.
Objective: Allow visitors to view specific components on my site depending on the URL. This is achieved using react-router-dom
.
It all works flawlessly locally but once it's live, it hits a 404 page. No errors when I test locally. I cannot recreate the problem locally so I don't really understand how to trouble shoot it.
Steps to recreate the problem:
Thanks to Phil who responded to my initial question which eventually led me to finding the answer here.
I may have asked the question incorrectly. Something I may have needed to include in my question is that I am deploying my site via cPanel. I would run npm run build
and then upload the build files to the root folder of my website.
The solution was to include a .htaccess
file with the following in the root folder of my site. For most people this would be public_html
.
Inside the .htaccess
file:
<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>
Once this file was uploaded, the routing worked just as it does locally. Hope it helps anyone else in a similar situation.