I have AngularJS webapp and start it with Tomcat 7. Using $locationProvider.html5Mode(true)
I remove hash '#' from url
localhost:8080/App/#/login -> localhost:8080/App/login
But now, when I refresh localhost:8080/App/login
I have 404 error
. How to configure Tomcat7 for localhost:8080/App/login
refresh?
app.config:
$urlRouterProvider.otherwise("/login/");
$stateProvider
.state('home', {
abstract: true,
templateUrl: 'dist/view/home.html',
controller: 'HomeCtrl'
});
$locationProvider.html5Mode(true);
index.html:
<head>
<meta charset="utf-8">
<script>
document.write('<base href="' + document.location + '" />');
</script>
</head>
Just write this in
htaccess
I dont have an
index.html
i haveindex.php
so i wroteindex.php
instead ofindex.html
Cheers!
I recently found a solution to this issue that doesn't involve switching to Apache2 with a .htaccess file.
In web.xml:
Then create a new file in WebContent called rewrite-404.jsp:
This will cause existing files in WebContent to be served as usual, and all requests that would normally return a 404 error to return index.html instead with a 200 (OK) response code.
1) Download urlrewritefilter-4.0.3.jar
2) Create WEB-INF folder in root directory where index.html is present.
3) Create lib folder in WEB-INF & put downloaded jar file into it.
4) Create web.xml file in WEB-INF . Put following code into it.
5) Create urlrewrite.xml file WEB-INF
put following code into it
This is using Tomcat 7 & Tucky's urlrewritefilter https://github.com/paultuckey/urlrewritefilter
@mhatch. To budle the routes to all go to index.html, you can try this rewrite rule :
This worked for me.
If you want to remove the hash (#), you must use rewrite function in server to serve the
index.html
for all path. If not, you will always receive404
error. It's mean you can redirect the404
toindex.html
.Apache: add a rewrite rule to the
.htaccess
file as show here:NGinx: use
try_files
, as described in Front Controller Pattern Web Apps, modified to serveindex.html
:IIS: add a rewrite rule to
web.config
, similar to the one shown here:In tomcat, you can add it into the web.xml:
After redirect the web path to
index.html
, the HTML5 History API will be used in Angular JS to control the web route. Our main task here is just letindex.html
be called in server.1) AngularJS
2) server side, just put .htaccess inside your root folder and paste this
Cheers....
It's because when you do a full reload you always send a a request to the server with the current url. Since Angular has not loaded yet it can't handle the route, and the server doesn't know anything about what routes exist in angular and not
Without html5mode Angular uses fragmented urls of the type
www.example.com/#/page
. # was originally used for anchor-links so by convention the fragment part of the url is completely ignored by the server. To the server that above request would be the same as justwww.example.com/
, which is probably where you index.html is located. So reloading your page will in this case get your index.html, which will load Angular, which will in turn handle the fragment part of the url (#/page).With html5mode however the fragment is hidden, and the url looks like a normal url,
www.example.com/page
. But when you reload the page the server doesn't get the fragment so it tries to find whatever is served under /page, which is probably nothing. Giving you a 404, Angular cannot load since index.html is not loaded, and you have a broken page.The usual way to fix this is to have the server serve index.html under all urls that does not match a static asset (or the pattern of one). So if you request say an image it will find it and return it ok, but if you request /page (and it does not exist as a static asset) then it returns index.html. Then Angular will start, read the url and trigger the correct route.
I have only done this in nginx so I don't know how Tomcat would do it, but the principle should be the same.