AWS S3
static website hosting
URL rewriting
web development
cloud storage

S3 static pages without .html extension

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Amazon Simple Storage Service (S3) is widely used for hosting static websites due to its scalability, durability, and affordability. A common requirement for enhancing user experience is to serve static pages without an .html extension. This approach mimics traditional website structures and is often preferred for aesthetic and SEO reasons. This article explores how to achieve it and covers related technical details.

Overview of S3 Static Website Hosting

Amazon S3 enables users to host static websites by configuring a bucket to act as a website endpoint. When a request is made, S3 retrieves and serves the stored files, typically HTML, CSS, JS, images, etc. By default, S3 serves files from the bucket with their full file paths, including extensions like .html. However, this is configurable.

Setting Up S3 Website Hosting

Here's a quick rundown of setting up S3 for static website hosting:

  1. Create an S3 Bucket: The bucket name should align with your desired domain (e.g., example.com).
  2. Enable Static Website Hosting: Configure the bucket's properties to enable static website hosting and specify an index document and an error document.
  3. Upload Content: Add your HTML, CSS, and JavaScript files to the bucket.
  4. Configure Permissions: Modify the bucket policy to allow public access to website files.
json
1{
2  "Version": "2012-10-17",
3  "Statement": [
4    {
5      "Sid": "PublicReadGetObject",
6      "Effect": "Allow",
7      "Principal": "*",
8      "Action": "s3:GetObject",
9      "Resource": "arn:aws:s3:::example.com/*"
10    }
11  ]
12}

Removing the .html Extension

To serve pages without the .html extension, there are several strategies:

1. S3 Object Names

Each object in S3 is identified by its key. By default, keys include .html for HTML pages (e.g., index.html). By structuring your keys properly, you can serve content without explicit extensions.

Example:

Instead of uploading a file as page1.html:

  • Use page1/ with an object key index.html inside it.

2. AWS CloudFront

AWS CloudFront, a content delivery network (CDN), can rewrite URLs for you. Implementing Lambda@Edge with CloudFront allows requests to be redirected appropriately:

  • Lambda@Edge Function: Set up a request handler function that modifies the URI to match S3 key structure.
javascript
1'use strict';
2exports.handler = (event, context, callback) => {
3    const request = event.Records[0].cf.request;
4    const uri = request.uri;
5    
6    if (!uri.endsWith('.html')) {
7        request.uri += '/index.html';
8    }
9    
10    callback(null, request);
11};

3. Route 53 Alias Records

Another advanced approach involves AWS Route 53 with alias records, which essentially point to another CloudFront distribution or ELB that can handle file requests intelligently.

SEO and User Experience Benefits

  • Improved Readability: URLs are more user-friendly without .html, making them easier to read and type.
  • SEO Optimization: Clean URLs are generally better received by search engines, potentially improving page rank.

Considerations

  • Caching: Ensure appropriate cache settings in CloudFront to manage versions effectively.
  • Security: Ensure bucket policies are carefully reviewed to prevent unintentional public access to sensitive data.
  • Cost: Evaluate potential costs associated with CloudFront or AWS Lambda, particularly at scale.

Summary Table

MethodKey FeaturesConsiderations
Object NamingSimple URL structure No additional costsManual management only
AWS CloudFront & Lambda@EdgeURL rewriting Highly customizable Leveraging cacheLambda costs Complex setup
Route 53 & Alias RecordsDirect DNS-based routing Utilizes AWS servicesLimited to AWS ecosystem

Conclusion

Serving S3-hosted static pages without an .html extension can significantly enhance user experience and SEO. Amazon S3, combined with services like AWS CloudFront and Route 53, offers robust solutions, but they come with their own sets of challenges and cost considerations. Understanding these options and implementing the right mix based on specific website requirements will help you achieve a cleaner, more effective URL management strategy.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.