angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

404 when manually changing URL in browser to defined router route

Open brookhutchinson opened this issue 9 years ago • 86 comments

OS?

Windows 10

Versions

[email protected]

Background

Angular App was created using angular-cli. Angular App contains a NavbarComponent that displays two menu items "Home" and "About". Angular App uses the Angular Router and a NavbarComponent to allow the user to navigate to two component views: HomeComponent View and AboutComponent View. The base route for the app is set inside index.html as . A Router Configuration File is defined and defines two routes, one route as the HomeComponent default route with a path: '' and one route as the AboutComponent Path with a path: 'about'. The ConfiguredRouterModule is exported from app.routing.ts The ConfiguredRouterModule is imported into app.module.ts. A directive is defined inside the AppComponent HTML Template. A routerLink directive is defined for each menu item link inside the NavbarComponent. To build the project I use the angular-cli command "ng build --prod". I copy files from "dist" folder to destination web server folder. Note that the destination web server folder is a sub folder under the root folder on the web server. Example "root/projects/router" and not just the "root" folder

NavbarComponent Menu Item Links correctly render Component Views

When a user clicks on either or the NavbarComponent menu item links "Home" or "About" the correct Component View displays inside the inside the AppComponent HTML Template

Issue

When a user manually changes the URL in the browser from "http://www.abc.com/projects/router/" to "http://www.abc.com/projects/router/about" instead of the AboutComponent displaying, a 404 Page Not Found Error is displayed. Note that this behavior does not produce a 404 Page Not Found Error when running the ng serve command in the development environment. This behavior only produces a 404 Page Not Found Error when building and deploying code to a web server.

After I execute the command ng build --prod I have to change the <base href="value"> from <base href="/"> to <base href="/projects/router/">.

Note that if I try to build the project using the angular-cli command ng build --prod --base-href projects/router, after executing command if I open the index.html file inside the "dist" folder I see that angular-cli changed the <base href="value"> from <base href="/" to <base href="C:/Program Files/Git/projects/router/>

My Questions

How do I build the Angular Project so that the <base href="value"> is set correctly? More importantly why is the Angular App giving a 404 Error if a user manually changes the URL in the browser to a valid router route path? Example http://www.abc.com/projects/router/about"

brookhutchinson avatar Feb 28 '17 16:02 brookhutchinson

You likely need to configure your server to rewrite unmatched URLs to your index.html.

mattdistefano avatar Feb 28 '17 18:02 mattdistefano

Thank you for your response. Unfortunately your suggestion is probably not related to my question.

The key to my question is that the route that is being navigated to in the browser URL is a valid defined route. My question is really why would a user get a 404 Error if the user manually changes the URL to a valid defined route?

If I test this behavior in the Development Environment by running the ng serve command I cannot recreate this behavior when running on http://localhost:4200. Only after I try to build and deploy the code using ng build --prod am I able to recreate the 404 Error by manually changing the URL to a valid defined route. Example: http://www.abc.com/projects/router/about

I can only guess that this issue has to do with using the ng build command and setting the <base href="/"> during the build process.

Even after manually changing the index.html (after the ng build --prod) from <base href="/"> to <base href="/projects/router/> the 404 behavior still happens when a user manually changes the URL to a defined route.

brookhutchinson avatar Feb 28 '17 18:02 brookhutchinson

@mattdistefano is right. the cli's dev server does this automatically. Angular's router uses the history api by default. when you use routerLink to change the route state it works fine because it is updating via this api. BUT if you build and use a simple server. the path /foo/bar doesn't exist on the server. so you need the server to always serve index.html so angular can resolve the route

deebloo avatar Feb 28 '17 18:02 deebloo

Thank you for the info on the differences between the Angular CLI DEV Server Environment and the PROD Server Environment files that are generated by Angular CLI when using ng build --prod.

This is very confusing. I still do not understand what I need to do to build and deploy my code to Production and have my Angular Router behave the same in Production as it does in Development.

Help me understand why anyone would use the Angular CLI ng build command if the Angular Router Behavior is different between the DEV Server Environment and the PROD Server Environment.

I would think that a developer should be able to run their code using the Angular CLI ng serve command and then test their code using the Angular CLI DEV Server Environment and expect the same behavior when the developer builds and deploys the same code to their PROD Server Environment.

How do I configure my Angular Router to behave the same in my PROD Environment as it does when running in the DEV Environment?

brookhutchinson avatar Feb 28 '17 19:02 brookhutchinson

@brookhutchinson If you want to use the history API routing, it's not angular that needs to be configured, it's your HTTP server.

If you can't change the server, you might try using the hash URL mode - see https://angular.io/docs/ts/latest/guide/router.html#!#browser-url-styles

mattdistefano avatar Feb 28 '17 19:02 mattdistefano

How do I configure my HTTP server to use the History API for Angular Routing? I do not want to use a URL hash strategy. My understanding is the the URL hash strategy is an "old school" approach to routing.

brookhutchinson avatar Feb 28 '17 19:02 brookhutchinson

Here is an example using express. Try looking at the accepted answer here. http://stackoverflow.com/questions/17456729/how-to-redirect-to-single-page-web-app-in-express-for-node.

deebloo avatar Feb 28 '17 19:02 deebloo

Maybe I am not building and deploying my Angular App correctly using Angular CLI?

All I am doing is...

  1. Execute ng build --prod command
  2. Change file dist/index.html <base href=value> from <base href="/"> to <base href="/projects/router">
  3. Copy all the files from my Angular Project "dist" folder to my PROD Web Server Destination Folder

brookhutchinson avatar Feb 28 '17 19:02 brookhutchinson

You may need (at least, want) to use the -bh option to ng build rather than changing the href in the built output.

mattdistefano avatar Feb 28 '17 19:02 mattdistefano

You need to edit the config of your webserver to serve all static content hits directly and send all 404s to the path "/" otherwise the request will not hit your angular application on any other request than "/" directly.

The reason the app works when you navigate to "/" then use the app normally is due to the fact that angular simply pushes state into the history API without reloading the actual site in your browser. But once you refresh you'll send a request with an URI to your webserver that does not exist and your webserver will respond with a 404.

grizzm0 avatar Feb 28 '17 20:02 grizzm0

Heya @brookhutchinson, the best advice I can give you is to check out https://angular.io/docs/ts/latest/guide/deployment.html. It explains why your production server needs to be configured and offers a fair number of example configurations for common servers.

filipesilva avatar Feb 28 '17 20:02 filipesilva

@filipesilva Thank you for the link that you provided to documentation surrounding the deployment of an Angular Project https://angular.io/docs/ts/latest/guide/deployment.html

I could be wrong, but the link that you provided addresses deployment of Angular Projects that are created with the Angular Quickstart. I am interested in learning how to properly use the Angular CLI to deploy an Angular Project that was created using the Angular CLI.

Can you please provide documentation for how to use the Angular CLI to deploy an Angular Project that was created using the Angular CLI and incorporates the Angular Router?

brookhutchinson avatar Mar 01 '17 03:03 brookhutchinson

@brookhutchinson it doesn't matter if the project is created with the cli or not. The results of both the cli and the quickstart example is static javascript.

deebloo avatar Mar 01 '17 03:03 deebloo

Just a short tip for those still bumping into this - a solution is to set the base href of your app for production deployment. Do this with ng build:

ng build --prod -bh /app/

... if your app files are in the app folder directly above index.html, e.g.

/root
    index.html
    /app
        ... app files ...

mbjelac avatar May 29 '17 11:05 mbjelac

Hello I have the same problem and I don't Know how to fix this ! I tried all the above propositions but in vain :( could anyone have the perfect solution for this issue !

XemaA avatar Jun 29 '17 16:06 XemaA

I also have this problem!

DavTho1983 avatar Aug 06 '17 11:08 DavTho1983

I also have this problem

leonardohorta avatar Sep 06 '17 21:09 leonardohorta

I solved this problem by uploading a .htaccess file to my hosting provider. I found it was not recognised when I included it as part of the dist - I think the build doesn't think it's important so gets rid of it - so I had to upload separately. The .htaccess code I just got from SO - you might have to play around with what works for your setup

DavTho1983 avatar Sep 07 '17 09:09 DavTho1983

@DavTho1983 you should be able to add .htaccess to your assets array in angular-cli.json similar to the favicon.

grizzm0 avatar Sep 07 '17 09:09 grizzm0

Ah! I will try that :)

DavTho1983 avatar Sep 07 '17 15:09 DavTho1983