Anand Sudhanaboina

Serve Static Pages on S3 Without .html Extension

Hosting static HTML pages generated by Jekyll or any other static site generator with pretty permalinks (the one’s without .html) on to S3 would yield into a 404. The reason is that S3 is an object store hence it doesn’t really look up for a .html version of a page’s permalink.

The solution is copying the file to S3 without the .html extension and explicitly setting the content type of the file, for example:

1
aws s3 cp index.html s3://bucket/index --content-type 'text/html'

By this S3 would send the file contents along with the content-type header set to text/html. To scale, I use the below script which automates this:

1
2
3
4
5
6
# In your public or _site directory
aws s3 cp ./ s3://bucket/ --recursive --exclude "*.html"

for file in $(find . -name '*.html' | sed 's|^\./||'); do
    aws s3 cp ${file%} s3://bucket/${file%.*} --content-type 'text/html'
done

Here’s what it does:

  1. Push all the files to S3 excluding all .html files
  2. Iterate all the .html files and push them to S3 without the extension and with content-type

Comments