Search code examples
reactjsdeploymentcreate-react-appnetlifynpm-build

Endpoint redirection in Netlify - Deploy inside a folder


I have a built using create-react-app and hosted in Netlify. I created a file called _redirects in folder 'public', but it did not work. The redirect is not happening.

/*    /index.html   200

I tried change the package.json:

"scripts": {
  ....
  "build": "react-scripts build && echo '/* /index.html  200' | cat >build/_redirects ",
  ...
}

I tried create a file /netlify.toml on the root (same level as package.json)

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200
  force = false

I read this stackoverflow question and I tried everything: Catch all redirect for create-react-app in netlify

I think the problem is in the way I am deploying it (because nothing is working) and I tested it in another application that I created a branch for deploy and the file _redirects worked perfectly.

Way I'm deploying on netlify - Image

Page: https://apuro-branch-main.netlify.app/

I'm testing the following direct link: https://apuro-branch-main.netlify.app/login

I tested putting in new branch and all files in the root folder (instead of inside a folder) and it worked.

I think that (/* /index.html 200) doesn't work if the deploy is not performed in the root directory (in my case, I did it inside a folder called apuro-frontend/).

So, I've already discovered that all my difficulty is related to the deployment inside a folder (apuro-frontend/). Although I understand the problem now, I still couldn't find a solution for this specific case.


Solution

  • Finally I managed to solve the problem! I believe that the deploy was storing some cache that prevented the correct redirection.

    It worked with:

    /* /index.html 200
    

    I had to re-link the deploy (I also tried clearing the cache using the 'Clear cache and retry deploy' command). But it worked even after I did the following steps:

    • Go to Deploy Settings
    • In the 'Repository' section, select 'Manage repository' , then 'Link to a different repository'.

    I selected the same repository I was using and put in the same commands, somehow this time it worked.

    This Stack Overflow question helped me: netlify deploy failed: git ref refs/heads/master does not exist or you do not have permission

    I had another problem, which I solved following the steps of the best answer and ended up solving both problems at once.