我正在使用以下 Lambda@Edge 代码,但不知道该怎么做。
以下代码使子目录根索引在 Cloudfront 上工作。
Cloudfront 是否应要求提供原产国?
'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 and params from the request
var olduri = request.uri;
// Match any uri that ends with some combination of
// [0-9][a-z][A-Z]_- and append a slash
var endslashuri = olduri.replace(/(\/[\w\-]+)$/, '$1/');
//console.log("Old URI: " + olduri);
//console.log("Params: " + params);
//console.log("Mid URI: " + miduri);
if(endslashuri != olduri) {
// If we changed the uri, 301 to the version with a slash, appending querystring
var params = '';
if(('querystring' in request) && (request.querystring.length>0)) {
params = '?'+request.querystring;
}
var newuri = endslashuri + params;
//console.log("No trailing slash");
//console.log("New URI: " + newuri);
const response = {
status: '301',
statusDescription: 'Permanently moved',
headers: {
location: [{
key: 'Location',
value: newuri
}]
}
};
return callback(null, response);
} else {
// Match any uri with a trailing slash and add index.html
newuri = olduri.replace(/\/$/, '\/index.html');
//console.log("File or trailing slash");
//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);
}
};