Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dev-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
ref: ${{ github.head_ref }}

- name: Push changes - Init Stage
run: git push origin -f dev:stage
run: git push origin -f dev:stage
40 changes: 40 additions & 0 deletions docs/cloudformation/geo-ca-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ Resources:
LambdaFunctionAssociations:
- EventType: origin-response
LambdaFunctionARN: !Ref SecHeadersLambdaEdgeArn
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt CloudfrontRootHandler.FunctionMetadata.FunctionARN
DefaultRootObject: index.html
HttpVersion: http2
WebACLId: !Ref WebAclArn
Expand All @@ -305,3 +308,40 @@ Resources:
ErrorCode: 404
ResponsePagePath: /404.html
ResponseCode: 404

CloudfrontRootHandler:
Type: AWS::CloudFront::Function
Properties:
AutoPublish: true
FunctionCode: >
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';
}

// Redirects requests to the www. url to the non www. url.
if (request.headers && request.headers.host && request.headers.host.value.includes('www.')) {
var newurl = request.headers.host.value.replace('www.', '')
var response = {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers:
{ "location": { "value": 'https://' + newurl + request.uri } }
}
return response;
}

return request;
}
FunctionConfig:
Comment: A function run on every request to geo.ca
Runtime: cloudfront-js-1.0
Name: root-handler