35

I deploy to netlify using ng build --prod, and the website works. But when I go to it, it automatically changes the link by adding /home onto the end. It still works, but then if I refresh the page or click any links to other pages, it doesn't work anymore. The reason the "/home" is added on is because I have a RouterModule set up that has home as it's initial path. Here is the code I have in my "app.module.ts" that sets up routes:

 NgbModule.forRoot(),
    RouterModule.forRoot([
      {
        path: '',
        redirectTo: '/home',
        pathMatch: 'full'
      },
      {
        path: 'home',
        component: HomeComponent
      },
      {
        path: 'terms-and-conditions',
        component: TermsAndConditionsComponent
      },
      {
        path: 'privacy',
        component: PrivacyPolicyComponent
      },
      {
        path: 'about',
        component: AboutComponent
      },
      {
        path: 'contact',
        component: ContactComponent
      },
      {
        path: 'team',
        component: TeamComponent
      },
      {
        path:'safety',
        component: SafetyComponent
      }
    ])
  ],
  providers: [],
  bootstrap: [AppComponent]

So why is it that the build doesn't work for the page linking? It just goes to a "404: page not found" and the console has no errors.

AT82
  • 71,416
  • 24
  • 140
  • 167
Betzel11
  • 559
  • 2
  • 6
  • 12
  • Maybe you have solved this since Sep 3rd, but the solution I provided should work, it did, at least for me :) – AT82 Sep 13 '18 at 11:03
  • @AJT_82 I will look into it now. – Betzel11 Sep 13 '18 at 18:43
  • @AJT_82 This works! Thank you so much man, I have spent so much time trying to figure this out and couldn't find solutions/explanations anywhere. – Betzel11 Sep 13 '18 at 19:21
  • @AJT_82 I have one more question. The official website will be hosted on apache. Do I need to undo these changes and do something else in order to get it working on that? – Betzel11 Sep 13 '18 at 20:11

5 Answers5

105

Old question, but for those who might stumble on it on how to enable angular routing in Netlify. Create a file _redirects in your src folder, add the following to it:

/*  /index.html 200

in your angular.json file add the following to projects.architect.build.options.assets

{
  "glob": "_redirects",
  "input": "src",
  "output": "/"
}

If you happen to use older version of Angular with angular.cli.json file, follow this: https://medium.com/@mgd4375/how-to-enable-angular-routing-in-a-netlify-deployment-with-the-angular-cli-e2eda69f1b5b where you do the equivalent change in angular.cli.json file, i.e add "_redirects" to the corresponding assets array.

AT82
  • 71,416
  • 24
  • 140
  • 167
13

Enable Angular Routing in Netlify deployment with the Angular CLI

Getting 404 on Refresh Page

  1. Open angular.json

  2. Under assets, add _redirects. This lets the resultant dist folder from a build include your soon-to-be _redirects file.<project-name>.architect.build.options.assets

    enter image description here

  3. In the src folder, add _redirects with the following line. Netlify uses this to redirect to index no matter what, allowing angular routing to take over. enter image description here

  4. Deploy! You’re done!

MADHUR GUPTA
  • 1,014
  • 10
  • 14
  • 7
    Just for a help. If someone got an error when build the project after adding this line. Use ```src/_redirects``` – shehan96 Sep 17 '21 at 09:54
  • Working perfectly in January 2023 with @shehan96 tip by adding "src/_redirects" instead of just "_redirects". – gregoryp Jan 27 '23 at 13:39
3

try Build command: ng build --prod --base-href ./ and add file to root project netlify.toml:

# A basic redirects rule
[[redirects]]
  from = "/*"
  to = "/index.html"
jon_eldiablo
  • 188
  • 2
  • 10
2

I had named my build directory wrongly from the original directory name i used to create the project

  1. In your angular.json check under the projects property for the name of the build directory, in my case bci

  2. So your Publish directory should be dist/bci on netlify

Winnipass
  • 904
  • 11
  • 22
2

Incase anyone's looking for a solution without having to create a _redirects file.

You can solve the issue by using the hash routing strategy. Your URLs will look somewhat like this name.netlify.app/#/hello/login and all you need to do is specify {useHash: true} when importing your RouterModule e.g

...
imports: [
   RouterModule.forRoot(routes, {useHash: true})
]
...
Adavize Aliyu
  • 217
  • 2
  • 9