-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (51 loc) · 1.8 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
45
46
47
48
49
50
51
52
53
54
55
56
// Replace with your domain
const domain = "aws.com"
/*
Redirect rules, each rule defines one or multiple locations for a subdomain of the domain defined above.
If you define multiple locations the key of each location defines the path to which the location is applied
You can define a default location for each subdomain using "*" as path.
The default redirect location for all requests is set in the handler below, replace it with yours.
*/
const redirects = {
"twitch": "https://twitch.tv/aws",
"console": {
"*": "https://console.aws.amazon.com",
"/lambda": "https://console.aws.amazon.com/lambda",
},
};
exports.handler = async (event, context) => {
let request = event.Records[0].cf.request;
var host = request.headers['host'][0].value;
// Default redirect location replace it with yours
var location = 'https://aws.amazon.com' + request.uri;
if (host.endsWith("." + domain)) {
var subdomain = host.replace("." + domain, "");
if (subdomain in redirects) {
var redirect = redirects[subdomain];
if(redirect.constructor == String) {
location = redirect;
} else if(redirect.constructor == Object) {
if(request.uri in redirect) {
location = redirect[request.uri];
} else if("*" in redirect) {
location = redirect["*"];
}
}
}
}
const response = {
status: '302',
statusDescription: 'Found',
headers: {
'location': [{
key: 'Location',
value: location,
}],
'cache-control': [{
key: 'Cache-Control',
value: "max-age=3600"
}],
},
};
return response;
};