Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '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);
- }
- };
Add Comment
Please, Sign In to add comment