forked from juspay/superposition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouting_function.js
More file actions
38 lines (30 loc) · 1.03 KB
/
Copy pathrouting_function.js
File metadata and controls
38 lines (30 loc) · 1.03 KB
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
// attached to behaviours 0, 1 in behaviour_settings.png in this directory
function handler(event) {
var request = event.request;
var uri = request.uri;
var headers = request.headers;
var prefix = '/open-source';
var newUri = uri;
var newUrl;
// Example: Redirect www.example.com to example.com
if (uri.startsWith(prefix + '/superposition/docs')) {
newUri = uri.substring(prefix.length); // Remove prefix
newUrl = 'https://' + headers.host.value + newUri;
var response = {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
'location': { value: newUrl }
}
};
console.log('redirecting to ' + newUrl);
return response;
}
// implies all urls are now /superposition/docs - just add the trailing slash if missing
if (!newUri.endsWith('/')) {
newUri += '/';
}
console.log('old request.uri is: + ' + uri + ' new request.uri is ' + newUri);
request.uri = newUri;
return request;
}