amazon-s3 - 使用 Cloudflare 的 S3 托管网站为任何路由返回 404 状态代码
问题描述
我有一个在 Cloudflare 后面运行良好的 S3 托管网站,其内容如下:
example.com/
工作正常
example.com/test
也可以,但是网络选项卡中的文档本身自然会返回 404,因为 /test 在 S3 上不存在。
这是 SEO 的问题,如何配置 Cloudflare 将 404 视为 200?
在 Cloudfront 中,我通常这样做:
但是我在 Cloudflare 中找不到对应的配置。这是否必须在 Cloudflare 工作人员中完成?在工人存在之前人们做了什么?
解决方案
事实证明,人们只是没有在工人之前使用 Cloudflare 在 S3 上托管,如果他们这样做了,他们并不关心/注意到他们的路线会返回 404。
无论如何,这是 Cloudflare 工作人员强制返回代码 200 的解决方案:
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
async function fetchAndApply(request) {
let originalResponse = await fetch(request)
const contentType = originalResponse.headers.get("Content-Type")
// Only bother with index pages (not assets)
if (contentType && contentType.includes("text/html")) {
// Force 404's from S3 to return as 200 to prevent Google indexing issues
let response = new Response(originalResponse.body, {
...originalResponse,
status: 200,
statusText: 'OK'
}
)
// Don't cache index.html
response.headers.set('Cache-Control', 'max-age=0')
return response
}
return originalResponse
}
推荐阅读
- apache-kafka - 尝试运行 Kafka JDBC 源连接器时出现 CORRUPT_MESSAGE
- javascript - Angular 2 如何将配置从 app.module.ts 传递给提供者
- python - 将列添加到 python numpy 数组
- regex - facelet 中的正则表达式模式不起作用
- angular-material - 使用 MatPaginator 时无法读取 null 的属性“长度”
- python-imaging-library - 如何将屏幕用作暗流的视频输入
- wordpress - WordPress分页的第7页重定向回首页
- sql - 连接两个表并添加另一列 SQL
- html - 想要在 AngularJS 中的标签上显示错误消息
- neo4j - 在具有多个 MERGE 的 neo4j 中求和 2 个图