-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (36 loc) · 2.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
exports.handler = (event, context, callback) => {
// Extract the request from the CloudFront event that is sent to Lambda@Edge
var request = event.Records[0].cf.request;
// Extract the URI from the request
var olduri = request.uri;
// Match any '/' that occurs at the end of a URI. Replace it with a default index
var newuri = olduri.replace(/\/$/, '\/index.html');
// for URLs you want to end without a /, do this...
//var newuri = newuri.replace(/donate$/, 'donate\/index.html');
if (!newuri.toLowerCase().endsWith(".html") && !newuri.toLowerCase().endsWith(".css")
&& !newuri.toLowerCase().endsWith(".css.map")
&& !newuri.toLowerCase().endsWith(".js")
&& !newuri.toLowerCase().endsWith(".json")
&& !newuri.toLowerCase().endsWith(".gif")
&& !newuri.toLowerCase().endsWith(".jpg")
&& !newuri.toLowerCase().endsWith(".jpeg")
&& !newuri.toLowerCase().endsWith(".png")
&& !newuri.toLowerCase().endsWith(".svg")
&& !newuri.toLowerCase().endsWith(".ico")
&& !newuri.toLowerCase().includes(".woff")
&& !newuri.toLowerCase().includes(".ttf")
&& !newuri.toLowerCase().includes(".txt")
&& !newuri.toLowerCase().includes(".xml")
&& !newuri.includes("?")) {
console.log("Adding .html to newuri");
newuri = newuri + ".html";
}
// Log the URI as received by CloudFront and the new URI to be used to fetch from origin
console.log("Old URI: " + olduri);
console.log("New URI: " + newuri);
// Replace the received URI with the URI that includes the index page
request.uri = newuri;
// Return to CloudFront
return callback(null, request);
};