Skip to content

Instantly share code, notes, and snippets.

@hieu-e
Created December 13, 2021 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hieu-e/8c7fc720fa41750652b750c76e823ec6 to your computer and use it in GitHub Desktop.
Save hieu-e/8c7fc720fa41750652b750c76e823ec6 to your computer and use it in GitHub Desktop.
CloudFront Functions redirect for React/SPA app under subdirectory.
function handler(event) {
var request = event.request;
var uri = request.uri;
// Check if SPA App 1 Direcotry
if (uri.startsWith('/my-app-1')) {
request.uri = '/my-app-1/index.html';
}
// Check whether the URI is missing a file name.
else 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment