preloader
image Reading time: 1 minute

Cloudfront Function for Static Site

Hugo pages by default have ugly urls turned off. Ugly urls end in index.html. For example this page would look like this:

  • with ugly urls: cloudfront-function-for-static-site.html
  • without ugly urls: cloudfront-function-for-static-site/

The problem is that AWS Cloudfront can only look at the root index.html but not in subfolders. This is a small AWS Cloudfront Function to rewrite and add index.html to every subfolder.


        function handler(event) {
            var request = event.request;
            var uri = request.uri;

            // Check whether the URI is missing a file name.
            if (uri.endsWith('/')) {
                request.uri += 'index.html';
            }
            // Check whether the URI is missing a file extension.
            else if (!uri.includes('.')) {
                request.uri += '/index.html';
            }

            return request;
        }

Share on:
AWS