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

@brookhutchinson @XemaA . i was able to fix the issue on my web hosting server. The issue is angular is a spa application and all routing are handle on the browser.so when you try to hit a route manually, your server tries to find the given html file on your server which does not exist therefore showing a 404 Not Found error.The work around i did is that when the route is manually hit and my server is about to throw a 404 not found error. I quickly redirect to the index.html file of my angular app which then renders the required route and view. so simply go change the 404 error of your server to redirect to your angular index.html file. see the sample link and behavior http://taskub.com/tasker-info

uviekelvin avatar Sep 20 '17 16:09 uviekelvin

@uviekelvin, Still i have same issues, Can you suggest me how to configure on IIS?

salih0313 avatar Sep 24 '17 06:09 salih0313

Hello everybody @brookhutchinson did you get a solution for this problem ? Can you help me ! Thanks

HabibFreelancer avatar Oct 05 '17 12:10 HabibFreelancer

@prince-sat

add this to your .htacccess

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

leandrit1 avatar Oct 09 '17 22:10 leandrit1

@salih0313 Did you figure it out how to fix this on IIS?

rbasniak avatar Oct 21 '17 20:10 rbasniak

To quick fix this issue you can add useHash property in your RouterModule (app module file). i.e. @NgModule( { imports:[RouterModule.forRoot(appRoutes,{useHash:true})] })

AkashVirani avatar Oct 31 '17 06:10 AkashVirani

@rbasniak I didn't fix on IIS. Simply using useHash property which mentioned above.

salih0313 avatar Oct 31 '17 07:10 salih0313

Thanks @xkLander This was working fine for me.

hidaytraj avatar Nov 04 '17 16:11 hidaytraj

Did anyone successfully fix this on IIS?

I am using an Angular Route as a Callback URL from a Payment Gateway but I keep getting a 404 on hitting my route in IIS.

kolexinfos avatar Nov 19 '17 08:11 kolexinfos

@kolexinfos go to you website on IIS, Error Pages, then select the 404 code and set it like the image below:

image

Click ok and it should look like this:

image

Now it should work.

rbasniak avatar Nov 19 '17 11:11 rbasniak

Nothing worked for me on IIS, for now I had to follow Angular deployment guide which suggested to add rewrite rule to web.config:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Angular Routes" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

alaa9jo avatar Nov 21 '17 15:11 alaa9jo

Exactly what I did to resolve my issue.

kolexinfos avatar Nov 21 '17 15:11 kolexinfos

For me this redirect in .htaccess solved the problem

RewriteEngine On ErrorDocument 404 /index.html

Nambio avatar Nov 22 '17 01:11 Nambio

I faced the same issue apply, all possible solution but finally, this solve my problem

export class AppRoutingModule {
constructor(private router: Router) {
	this.router.errorHandler = (error: any) => {
		this.router.navigate(['404']); // or redirect to default route
	}
  }
}

Hope this will help you to resolve 404 error

mejayantpatil avatar Dec 05 '17 08:12 mejayantpatil

Please see the following article https://stackoverflow.com/questions/38054707/angular-2-azure-deploy-refresh-error-the-resource-you-are-looking-for-has-been

For Azure, I created the web.config file and added to my /src folder, also added an entry for web.config in assets array inside angular-cli.json and it fixed my problem :)

sainture avatar Dec 14 '17 03:12 sainture

Please help me out i canot understand what i have to do to solve this issue, i dont want to add useHash as true, please help me out

aakash1147 avatar Dec 15 '17 09:12 aakash1147

useHash is a different thing - I don't think it's necessary in Angular 5. Your problem isn't routing within Angular, it's your deployment to your hosting provider. This issue is solved on this thread.

DavTho1983 avatar Dec 15 '17 10:12 DavTho1983

@DavTho1983 i am using angular cli 1.0.0 version is there a way to solve this issue in this version only

aakash1147 avatar Dec 15 '17 12:12 aakash1147

You should probably update to at least 1.5 I believe - with cli 1.0 I think there are still issues around routing, but there are so many SO questions around this and the issue shouldn't be with your routing within Angular if it works for you in the ng serve environment. The issue is with how it's being deployed by the server by your host - that's the part you need to understand better

DavTho1983 avatar Dec 15 '17 13:12 DavTho1983

Hi All,

I have deployed my angular app in IIS and by configured the rewrite rule in web.config I am able to fall in the home page of application(Which is default route). But for other pages, the routing is not working. Clicking on links in home page suppose to follow the routing rule and should navigate to the respective components. Instead of that, it is redirecting to the same home page url. Does anyone here face similar issue? If so, could you please help to resolve the same..?

Thanks in advance. :)

Jithesh-Menon avatar Dec 18 '17 14:12 Jithesh-Menon