I deployed a React website using GoDaddy (http://normaned.com), and I’m having trouble with my react-router
routing not working upon refresh. Clicking on links works as expected, but if a page is refreshed a 404 page is displayed. I’m using BrowserRouter
from react-router
.
The GoDaddy hosting plan is “Economy Windows Hosting with Plesk”. Unfortunately, I’m not the one who set up the hosting service, and I’m unsure if I can change from Plesk to cPanel without extra monetary cost… or if that would even be a way to get closer to solving my problem (i.e., Windows vs Linux hosting).
EDIT 10/19: I now realize that the server is a Windows IIS server, and I need a web.config
file (not a .htaccess
file). Though, I’m still unsure of what code needs to go in the web.config
file.
Here is my GitHub repo for the website: https://github.com/jenna-m/norman-ed
I have tried suggested methods I’ve found on StackOverflow, the GoDaddy help forum, and elsewhere, but it’s still not working. Here are some things that I’ve tried:
https://stackoverflow.com/a/40591955/11995771
https://gist.github.com/alexsasharegan/173878f9d67055bfef63449fa7136042
I’ve tried adding the following to an .htaccess
file in the public_html
root directory:
<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>
I created the .htaccess
file directly in Plesk, making sure that no extra characters were added (as suggested in this article (https://www.godaddy.com/help/what-is-htaccess-2504)
After trying to solve the problem for a bit, I realized that this might have to do with the fact that I’m using Plesk vs cPanel. So, I found these potential work arounds:
I thought that either of these solutions would work, but they didn’t.
I found this post from Plesk support (https://support.plesk.com/hc/en-us/articles/115000063989-Does-Plesk-support-Apache-web-server-for-Windows-), which lead me to this Microsoft article (https://blogs.msdn.microsoft.com/azureossds/2015/04/23/converting-apache-htaccess-rules-to-iis-web-config-using-iis-manager-for-azure-websites/).
4
Answers
It turns out that I was using a Windows IIS server. I'm new to the world of web servers and didn't know that I was working with an IIS server.
It turns out that I needed a
web.config
file to make my URL redirects work, and not a.htaccess
file, like I was trying to use initially.This is the content (found from this StackOverflow answer) of my
web.config
file:If you are using and Apache server the problem might be your .htaccess file, when you are using React router the routes that are created or declared in React do not exist in the server, therefore we have to configure the requests so that each one goes to index.html and show a 404 in React when the page is not found.
According to the create-react-app documentation:
If you are using Apache HTTP Server, you need to create a .htaccess file in the public folder that looks like this:
Link to create-react-app documentation:
https://create-react-app.dev/docs/deployment
You can read more about the creation of a 404 page in React in the React router: documentation:
https://reacttraining.com/react-router/web/example/no-match
Try specifying your homepage in your package.json file
Then run
or
Follow these steps and you will be fine
Specify your homepage in your package.json file like this
2.Build your app by running
yarn build
ornpm run build
3.Then copy the contents of your build folder and paste them on public_html folder in your cpanel
3.Go to your Cpanel file manager public_html folder and edit .htaccess file or create new one if it does not exist. and paste this there
Note the .htaccess for this should be at the same level with the contents of the build folder.