I have a create-react-app that runs perfectly on my local host, however, when I try to deploy into cPanel (I want to deploy into subdirectory as the main domain already have other contents), Only the front page works fine and all other links are broken.
Example code below:
<BrowserRouter >
<TopBar/>
<Switch>
<Route exact path="/start" component={start}/>
<Route path="/start/page1" component={page1}/>
<Route path="/start/page2" component={page2}/>
</Switch>
</BrowserRouter>
I have set "homepage" as "http://mydomain/start/" in package.json as mentioned in https://create-react-app.dev/docs/deployment.
Everything works fine on my local host until I run "npm run build" and upload to cPanel subdirectory. Which works fine in "http://mydomain/start/" but in "http://mydomain/start/page1" and "http://mydomain/start/page2" it shows 404 Error.
Is there any configuration I have missed? Thanks in advance.
2
Answers
So what I would suggest is that you go into Cpanel and created a subdomain, if you haven’t already. You do it here.
Then you chose your domain and add the subdomain, as seen here:
After that, all you need to do is go to the File Manager, see your subdomain there and add your files there along with the .htaccess file.
Now, the .htaccess can vary a lot depending on what you are trying to do, but saying you want to start with "page1", and I am guessing that, that page1 is an html file after you
npm build
, you can do as following*The DirectoryIndex is the main file *
Let me know if it helped
Have you done point 6 in here? Essentially I think the 404 is happening because requests to
/start/page1
are not actually reaching your react app at all, they are trying to reach that location on the server which doesn’t contain anything.There will be some kind of config that allows requests to all paths starting with
/start
to be routed to the react app, at which point the react routing will kick in. I believe that link might help you.