我已经设置了一个S3存储桶来托管我的网站并设置CloudFront,以指向我的S3存储桶。所有请求都发送到CloudFront将转到我的S3存储桶。我要做的是设置重定向规则仅适用于我的S3存储桶中的一个文件之一。S3中有一个JavaScript文件/folderA/index.js
。我想应用规则将该文件上的请求重定向到另一个文件,即 /folderB/index.js
。我该如何设置?我是否需要在S3桶或CloudFront上进行设置?
我认为您不能为文件定义S3重定向规则。我相信它是一个水桶。
您可以将/folderB/index.js
内容复制到/folderA/index.js
中,因此不需要重定向。
另外,您可以设置查看器请求lambda@edge函数,如果路径等于 /folderA/index.js
类似:
'use strict';
exports.handler = (event, context, callback) => {
/*
* Generate HTTP redirect response with 301 status code and Location header.
*/
const request = event.Records[0].cf.request;
const uri = request.uri;
if (ur.endsWith('/folderA/index.js`) {
const redirectTo = //where you want to redirect
console.log(`Redirecting ${uri} with to: ${redirectTo}`);
const response = {
status: '301',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: redirectTo,
}],
},
};
callback(null, response);
} else {
callback(null, request);
}
};