Guest User

Untitled

a guest
Apr 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. 'use strict';
  2. exports.handler = (event, context, callback) => {
  3.  
  4. // Extract the request from the CloudFront event that is sent to Lambda@Edge
  5. var request = event.Records[0].cf.request;
  6.  
  7. // Extract the URI and params from the request
  8. var olduri = request.uri;
  9.  
  10. // Match any uri that ends with some combination of
  11. // [0-9][a-z][A-Z]_- and append a slash
  12. var endslashuri = olduri.replace(/(\/[\w\-]+)$/, '$1/');
  13.  
  14. //console.log("Old URI: " + olduri);
  15. //console.log("Params: " + params);
  16. //console.log("Mid URI: " + miduri);
  17.  
  18. if(endslashuri != olduri) {
  19. // If we changed the uri, 301 to the version with a slash, appending querystring
  20. var params = '';
  21. if(('querystring' in request) && (request.querystring.length>0)) {
  22. params = '?'+request.querystring;
  23. }
  24. var newuri = endslashuri + params;
  25.  
  26. //console.log("No trailing slash");
  27. //console.log("New URI: " + newuri);
  28.  
  29. const response = {
  30. status: '301',
  31. statusDescription: 'Permanently moved',
  32. headers: {
  33. location: [{
  34. key: 'Location',
  35. value: newuri
  36. }]
  37. }
  38. };
  39. return callback(null, response);
  40. } else {
  41. // Match any uri with a trailing slash and add index.html
  42. newuri = olduri.replace(/\/$/, '\/index.html');
  43.  
  44. //console.log("File or trailing slash");
  45. //console.log("New URI: " + newuri);
  46.  
  47. // Replace the received URI with the URI that includes the index page
  48. request.uri = newuri;
  49.  
  50. // Return to CloudFront
  51. return callback(null, request);
  52. }
  53. };
Add Comment
Please, Sign In to add comment