Configuring a subdirectory with AWS using CloudFront and Route 53
Host your documentation with a /docs subdirectory using AWS CloudFront and Route 53.
1
2
3
export const handler = async (event) => {
const request = event.Records[0].cf.request;
// update if your subdirectory is not /docs
const subdirectory = '/docs';
// update with your proxy URL below
const target = new URL('<proxy URL you got from GitBook>');
// rewrite: /docs* -> proxy.gitbook.site
if (request.uri.startsWith(subdirectory)) {
request.uri = target.pathname + request.uri.substring(subdirectory.length);
// Remove trailing slash if present
if (request.uri.endsWith('/')) {
request.uri = request.uri.slice(0, -1);
}
request.origin = {
custom: {
domainName: target.host,
port: 443,
protocol: 'https',
path: '',
sslProtocols: ['TLSv1.2'],
readTimeout: 30,
keepaliveTimeout: 5,
customHeaders: {},
},
};
request.headers['host'] = [{ key: 'host', value: target.host }];
request.headers['x-forwarded-host'] = [{ key: 'x-forwarded-host', value: target.host }];
}
return request;
};4
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"edgelambda.amazonaws.com",
"lambda.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}5
6
Setting
Value
Setting
Value
7
8
9
10
Troubleshooting
Last updated
Was this helpful?