Created
          September 21, 2020 12:41 
        
      - 
      
- 
        Save mucahitnezir/df229e54a839295fde1a22c1dfa37d84 to your computer and use it in GitHub Desktop. 
    Lambda function for url rewriting: lambda@edge function
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const appendToDirs = 'index.html'; | |
| const regexSuffixless = /\/[^/.]+$/; // e.g. "/some/page" but not "/", "/some/" or "/some.jpg" | |
| const regexTrailingSlash = /.+\/$/; // e.g. "/some/" or "/some/page/" but not root "/" | |
| exports.handler = (event, context, callback) => { | |
| const { request } = event.Records[0].cf; | |
| const { uri } = request; | |
| // Append ".html" to origin request | |
| if (uri.match(regexSuffixless)) { | |
| request.uri = uri + '/' + appendToDirs; | |
| callback(null, request); | |
| return; | |
| } | |
| // Append "index.html" to origin request | |
| if (uri.match(regexTrailingSlash)) { | |
| request.uri = uri + appendToDirs; | |
| callback(null, request); | |
| return; | |
| } | |
| // If nothing matches, return request unchanged | |
| callback(null, request); | |
| }; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment