'use strict';


/**
* Redirects URLs to default document. Examples:
*
* /blog            -> /blog/index.html
* /blog/july/      -> /blog/july/index.html
* /blog/header.png -> /blog/header.png
*
*/


let defaultDocument = 'index.html';


exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;


if(request.uri != "/") {
let paths = request.uri.split('/');
let lastPath = paths[paths.length - 1];
let isFile = lastPath.split('.').length > 1;


if(!isFile) {
if(lastPath != "") {
request.uri += "/";
}


request.uri += defaultDocument;
}


console.log(request.uri);
}


callback(null, request);
};

'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 from the request
var olduri = request.uri;


// Match any '/' that occurs at the end of a URI. Replace it with a default index
var newuri = olduri.replace(/\/$/, '\/index.html');


// Log the URI as received by CloudFront and the new URI to be used to fetch from origin
console.log("Old URI: " + olduri);
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);


};

if ((window.location.href.endsWith("/") && !window.location.href.endsWith(".com/"))) {
window.location.href = window.location.href + "index.html";
}
else {
document.write("<Your 403 error message here>");
}

import json
import boto3


s3_client = boto3.client("s3")


def lambda_handler(event, context):


for f in event['Records']:


bucket_name = f['s3']['bucket']['name']
key_name = f['s3']['object']['key']
source_object = {'Bucket': bucket_name, 'Key': key_name}


file_key_name = False


if key_name[-10:].lower() == "index.html" and key_name.lower() != "index.html":
file_key_name = key_name[0:-10]
elif key_name[-9:].lower() == "index.htm" and key_name.lower() != "index.htm":
file_key_name = key_name[0:-9]
        

if file_key_name:
s3_client.copy_object(CopySource=source_object, Bucket=bucket_name, Key=file_key_name)

function handler(event) {
var request = event.request;
var uri = request.uri;
    

// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
request.uri += '/index.html';
}


return request;
}