Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3 static pages without .html extension

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?

like image 858
Tyler Avatar asked May 05 '14 01:05

Tyler


People also ask

Can Amazon S3 run a static website?

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/ .

Can you host dynamic websites on S3 what about Static websites?

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.


2 Answers

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.

like image 132
André Lucas Avatar answered Sep 29 '22 15:09

André Lucas


In general on Amazon S3, to create clean URLs you can:

  1. 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.

  2. 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.

like image 30
jamtin Avatar answered Sep 29 '22 14:09

jamtin