没有 .html 扩展名的 S3 静态页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23463679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
S3 static pages without .html extension
提问by Tyler
In serving a static site off of Amazon S3, I'm wondering how to get rid of the .html
file extensions for each page.
在为 Amazon S3 之外的静态站点提供服务时,我想知道如何摆脱.html
每个页面的文件扩展名。
Right now I have:
现在我有:
mysite.com/ # works fine, serves index.html
mysite.com/mypage.html # works fine
mysite.com/mypage # doesn't work
The error for /mypage
shows:
显示错误/mypage
:
404 Not Found
Code: NoSuchKey
Message: The specified key does not exist.
Key: mypage
RequestId: 1089D7A26EFED9AD
HostId: Ud8cFy8Zl1mJ+oFjFOmU1Xacq9+v70KuaJfOc4nFMEPhd66AkLhr4Pj5u0QH6Gog
I have tried setting the Content-Type
to text/html
, as per this post, but it doesn't fix the problem for me.
我已经尝试将 设置Content-Type
为text/html
,根据这篇文章,但它并没有为我解决问题。
How do I get /mypage
to serve the file at /mypage.html
on S3?
如何在 S3 上/mypage
提供文件/mypage.html
?
回答by André Lucas
I just had this problem and I'd like to share the solution:
我刚刚遇到了这个问题,我想分享解决方案:
You have to set the meta data of the file Content-type
to text/html
AND rename the file removing the .html
of the end.
你必须设置文件的元数据Content-type
来text/html
并重新命名文件中删除.html
结束。
That way you will be able to reach every page without the file extension.
这样您就可以在没有文件扩展名的情况下访问每个页面。
回答by jamtin
In general on Amazon S3, to create clean URLs you can:
一般来说,在 Amazon S3 上,要创建干净的 URL,您可以:
Upload the page file with a "clean" name, e.g.
mypage
and set theContent-Type
set totext/html
(as the post you linked to described). You must rename the file on your system before you upload it to have no extension, or renameit without the extension on S3 after uploading. The file's name on S3 must nothave an extension.Create a folder with the "clean" name and upload the page file to that folder with its name set to the default index document, e.g.
index.html
. You need to check what the default index document name is. This is set when you configure your bucket as a website, but can be changed later.
上传具有“干净”名称的页面文件,例如
mypage
并将设置Content-Type
设置为text/html
(如您链接到的帖子所述)。您必须在上传之前将系统上的文件重命名为无扩展名,或者上传后在 S3 上重命名不带扩展名。S3 上的文件名不能有扩展名。创建一个名为“clean”的文件夹,并将页面文件上传到该文件夹,并将其名称设置为默认索引文档,例如
index.html
. 您需要检查默认索引文档名称是什么。这是在您将存储桶配置为网站时设置的,但可以稍后更改。
If you can't make the above work you can upload a new zero-byte object with the name key mypage
and then set a page redirect by specifying the Website Redirect Location
key with a value mypage.html
in the metadata during the upload process. See Configuring a Web Page Redirect in the Amazon S3 documentation.
如果您无法完成上述工作,您可以使用名称键上传一个新的零字节对象,mypage
然后在上传过程中通过在元数据中指定Website Redirect Location
键值来设置页面重定向mypage.html
。请参阅 Amazon S3 文档中的配置网页重定向。
You could also copy the file to a new object named mypage
with Content-Type
set to text/html
.
你也可以将文件复制到一个名为新对象mypage
与Content-Type
设置为text/html
。
回答by BrotherDonkey
As some have said already, delete the file extension, but then simply follow the following steps:
正如有些人已经说过的那样,删除文件扩展名,然后只需按照以下步骤操作:
- Go to your Amazon S3 bucket.
- Select the checkbox next to the file you'd like to have linking properly.
- Click "Properties" on the right, and a new pane will show up. It'll say "Object: filename"
- Click the Metadata tab, change from default, which for me was "binary/octet-stream"; new value should be "text/html".
- Save.
- 转到您的 Amazon S3 存储桶。
- 选中要正确链接的文件旁边的复选框。
- 单击右侧的“属性”,将显示一个新窗格。它会说“对象:文件名”
- 单击元数据选项卡,从默认更改,对我来说是“二进制/八位字节流”;新值应该是“text/html”。
- 节省。
And as people have said, make sure your new links in your html don't contain the .html file extension anymore. This worked for me.
正如人们所说,确保 html 中的新链接不再包含 .html 文件扩展名。这对我有用。
回答by Tyler
Creating a folder at /mypage
and putting index.html
inside of it wasn't working for me. It turns out that this was because the Bucket's "Index Document" setting had been changed to myindex.html
:
创建一个文件夹/mypage
并将其放入index.html
其中对我不起作用。事实证明,这是因为 Bucket 的“索引文档”设置已更改为myindex.html
:
Bucket Properties --> Static Website Hosting --> Enable Website Hosting --> Index Document
This was actually being applied to all subfolders too, so that it wasn't looking for /mypage/index.html
when on the /mypage
route; it was looking for /mypage/myindex.html
instead.
这实际上也应用于所有子文件夹,因此它不会/mypage/index.html
在/mypage
路由上查找;它正在寻找/mypage/myindex.html
。
I simply changed the myindex.html
setting back to index.html
, and the standard folder structure works. It would have worked equally as well to use myindex.html
files everywhere with that setting in place, but that seemed confusing for no real gain.
我只是将myindex.html
设置改回index.html
,并且标准文件夹结构有效。myindex.html
在该设置到位的情况下在任何地方使用文件也同样有效,但这似乎没有真正的好处。
I still don't know why setting the Content-Type
to text/html
doesn't work -- seems like it should as that is mentioned in several places.
我仍然不知道为什么设置Content-Type
totext/html
不起作用 - 似乎应该像在几个地方提到的那样。
Anyway, the problem is solved by changing the Bucket's Index Document
and all subfolders to use index.html
.
无论如何,通过将 BucketIndex Document
和所有子文件夹更改为使用index.html
.
回答by captainblack
With the AWS CLI:
使用 AWS CLI:
Assuming that you have removed the extension from the filename and set up AWS CLI with your account, you can set the meta data of the file by running this command in your console:
假设您已从文件名中删除扩展名并使用您的账户设置 AWS CLI,您可以通过在控制台中运行以下命令来设置文件的元数据:
aws s3 cp /path/to/file s3://yourwebsite.com --content-type 'text/html' --acl public-read
回答by Nirmal Bajpai
Very Simple and Tested Solution. :
非常简单且经过测试的解决方案。:
- Rename file, Remove Extension (.html) from file name. (ex. change gallery.html to gallery).
- Edit Properties of File. Add Metadata key (Content type = text/html and content language = html).
- Save and test from browser url like websiteurl.com/gallery.
- 重命名文件,从文件名中删除扩展名 (.html)。(例如,将gallery.html 更改为gallery)。
- 编辑文件属性。添加元数据键(内容类型 = text/html 和内容语言 = html)。
- 从浏览器 url 保存并测试,如 websiteurl.com/gallery。
回答by Gianluca Casati
Another possible solution is to use Lambda@edge power, documentation reference here.
另一种可能的解决方案是使用 Lambda@edge power,文档参考这里。
回答by passionatedevops
If you want to upload json file. Save the json file without extension in your local machine.
如果你想上传json文件。将不带扩展名的 json 文件保存在本地计算机中。
From AWS CLI use below command :
从 AWS CLI 使用以下命令:
aws s3 mb s3://<s3bucketname>
#output
make_bucket: s3bucketname
aws s3 cp path\to\local\file s3://<s3bucketname> --content-type 'application/json' --acl public-read
#output
upload: .\<filename> to s3://<s3bucketname>/<filename>
now you can access the file using url:
现在您可以使用 url 访问该文件:
https://<s3bucketname>.s3.amazonaws.com/<filename>
https://<s3bucketname>.s3.amazonaws.com/<filename>