如何在AWS Cloudfront(S3)上获取干净的URL? [英] How to get clean URLs on AWS Cloudfront (S3)?

查看:251
本文介绍了如何在AWS Cloudfront(S3)上获取干净的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的静态网站托管在AWS S3上,并将Cloudfront作为CDN,我想知道如何使干净的URL有效。

I'm hosting my static website on AWS S3, with Cloudfront as a CDN, and I'm wondering how I can get clean URLs working.

我目前必须访问 example.com/about.html 以获得关于页面。我更希望 example.com/about 以及其他所有页面。另外,我必须这样做,因为我的规范网址已设置了元标记和搜索引擎,并且更改它们会花费很多。

I currently have to go to example.com/about.html to get the about page. I'd prefer example.com/about as well as across all my other pages. Also, I kind of have to do this because my canonical URLs have been set with meta tags and search engines, and it's gonna be a bit much to go changing them.

我没有看到Cloudfront中的某个设置吗?

Is there a setting in Cloudfront that I'm non seeing?

我有两个选择

首先是将文件 .html 修剪掉,然后再上传到S3和然后在该文件的http中编辑内容标题。这样做可能效果不错,但是我无法弄清楚如何从命令行编辑内容标头,而我正在编写推送网站更新 bash脚本。

First is trimming .html off the file before uploading to S3 and then editing the Content Header in the http for that file. This might work beautifully, but I can't figure out how to edit content headers from the command line, where I'm writing my "push website update" bash script.

第二,并利用S3的功能来识别根默认文件,通常是index.html。可能是一个很好的方法,但是它使我的本地测试变得充满挑战,并且在不适合我的URL上留下了斜线。

Second is detailled by Matt below and leverages S3's feature that recognizes root default files, usually index.html. Might be a great approach, but it makes my local testing challenging, and it leaves a trailing slash on the URLs which doesn't work for me.

推荐答案

在S3(以及扩展名为CloudFront)中托管网站时,可以将S3配置为在请求目录时加载默认文件。这就是所谓的索引文档。

When you host your website in S3 (and by extension CloudFront), you can configure S3 to have a "default" file to load when a directory is requested. This is called the "index document".

例如,您可以配置S3以将 index.html 加载为默认文件。这样,如果请求是针对 example.com/abc / 的,则它将加载 abc / index.html

For example, you can configure S3 to load index.html as the default file. This way, if the request was for example.com/abc/, then it would load abc/index.html.

执行此操作时,如果他们要求 example.com/abc/123.html ,则它将投放 abc / 123.html 。因此,默认文件仅在请求文件夹时适用。

When you do this, if they requested example.com/abc/123.html, then it will serve up abc/123.html. So the default file only applies when a folder is requested.

要满足您对 example.com/about / ,您可以使用默认文件 index.html 配置存储桶,并在其中添加 about / index.html

To address your request for example.com/about/, you could configure your bucket with a default file of index.html, and put about/index.html in your bucket.

更多信息可以在Amazon文档中找到:索引文档支持

More information can be found in Amazon's documentation: Index Document Support.

这篇关于如何在AWS Cloudfront(S3)上获取干净的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆