In serving a static site off of Amazon S3, I'm wondering how to get rid of the .html
file extensions for each page.
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:
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.
How do I get /mypage
to serve the file at /mypage.html
on S3?
After you create a bucket, you can enable static website hosting for your bucket. You can create a new bucket or use an existing bucket. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ .
For hosting a dynamic website on AWS, you need to use EC2 product. S3 is only used for storage and static website hosting. Other than EC2, you can also use Lightsail, which is basically a VPS. For hosting on EC2, you will need to launch an empty and install LAMP or any PHP based stack you have on the server.
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.
That way you will be able to reach every page without the file extension.
In general on Amazon S3, to create clean URLs you can:
Upload the page file with a "clean" name, e.g. mypage
and set the Content-Type
set to text/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 rename it without the extension on S3 after uploading. The file's name on S3 must not have 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.
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.
You could also copy the file to a new object named mypage
with Content-Type
set to text/html
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With