Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs html5mode refresh page get 404

Tags:

I've created an app using AngularJS with GAE (Google App Engine – Java).

I want to convert it compatible with SEO.

index.html

  • <meta name="fragment" content="!" />
  • <base href="/">

and the body is loaded dynamically through <div ui-view></div>.

app.js

$locationProvider.html5Mode(true);

The url works fine but when I refresh page I get 404 error.

Do you have any idea, what this causes?

like image 478
user2625111 Avatar asked Jul 27 '13 08:07

user2625111


People also ask

How to do a page refresh on angular?

We will look at the best method to do a page refresh on Angular. First, we will have to head to our editor, open the terminal most preferably Visual Studio Code and create the app-routing.module.ts using the ng generate function.

What does html5mode do in angular?

By setting html5mode to true in an AngularJS application it removes the hash (#) prefix from the angular virtual urls, a side effect of this is that if you try to navigate directly to a deep link in your angular app or refresh an internal page you may get a 404 page not found error.

Why can't I see the angular url in the web page?

This is because the web server receiving the request looks for a resource matching the full url on the server, which doesn't exist because the angular portion of the url refers to a route in your angular application and needs to be handled in the client browser.

How to create a contact url in AngularJS?

So we need to use the “ # ” mechanism which Angularjs 1 was using. for example, we need to make our urls look like “ http://localhost/#/contact ” 1). The first thing we need is to import the below two modules from “ @angular/common ” like below in the “ app.module.ts ” file


1 Answers

I'm sure you've already solved this, but for anybody else running into this issue:

Basically AngularJS's $locationProvider.html5mode(true) makes use of HTML5's history.pushState(), which artificially changes the user's history and address bar URL without reloading the page. If you create a route (in Angular) for /about, but don't have any matching route on the server, you will run into the issue where reloading the page reveals the fact that it's not there on the server. The simplest solution is to mirror your entry point for your app (/index.html?) for all routes accessible by your app.

See: https://stackoverflow.com/a/16570533/1500501

like image 156
Michael Tang Avatar answered Sep 20 '22 16:09

Michael Tang