AngularJS html5mode 404 not found


Redirect 404 not found page to index.html.
Included simple http static server, apache htaccess and nginx config

If AngularJS 1.1.5 must add the “base” html tag in “head” like

1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html lang="en" ng-app="LabApp">
<head>
<meta charset="utf-8">
<title>Sample</title>
<base href="/">
</head>
<body></body>
</html>

NodeJS HTTP Static Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Install: npm install node-static
// Usage : Place into www root directory and node server.js
var static = require('node-static'),
fileServer = new static.Server('./'),
port = process.env.PORT || 5000;

require('http').createServer(function (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response, function (err, result) {
if (err && (err.status === 404)) {
fileServer.serveFile('/index.html', 200, {}, request, response);
}
});
}).resume();
}).listen(port);

Apache HTTPD

1
2
3
4
5
6
7
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteCond %{REQUEST_URI} !.*\.(css¦js|html|png)
RewriteRule (.*) index.html [L]

Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 80;
charset utf-8;
server_name subdomain.example.com;
root /home/user/subdomain.example.com;

location ~* \ (gif|jpg|png) {
expires 30d;
}

location / {
index index.html index.htm;
try_files $uri $uri/ /index.html?$args;
}

location ~ /\.ht {
deny all;
}
}