Default HTML files for subfolders with AWS

I have a set of static websites that I run on AWS (Amazon Web Services) and a bunch of sites that are essentially apps running on servers on Azure. The latter ones? No real issues, after all they’re written in C# and .NET which I’m kinda familiar with. The sites on AWS? Well, it’s been a hoot over the years.

The first big issue I had was the fact that file and folder names on AWS are case-sensitive. Me? A typical “insensitive” Windows guy: foobar.txt and FooBar.txt are the same file. It seemed that link requests coming from Google (say) would be what I might call normal-cased (the names were the same case that I’d originally labeled them) and sometimes lower-case. My first solution to that was to replicate the static site files with lower-case names in the same folders. Of course, even to this semi-IT Guy, that was just a crap way of doing things, so my eventual solution was to write a Lambda@Edge function that converted requests for HTML files to lower-case names. After AWS had an update that introduced request/response functions in CloudFront, I converted the Lambda function to a CloudFront one.

Enter the next big issue. AWS has a weird system for hosting static sites. Basically you create an S3 bucket for the site and label it as a static website. In doing so, you also specify the home or default page of the website, such as index.html. Once you’ve set up the DNS for your domain name to point to this bucket, it’ll automatically download that index document if you visit the domain. The problem I noticed only recently was that this index document name does not apply to subfolders. So, for instance, if my domain was example.com, I could navigate to example.com and get example.com/index.html automatically, but if I tried to go to example.com/foobar I’d get a 404 error, despite having a correctly named index.html in that subfolder.

Time to change my lower-case function.

Basically this is what I wanted to happen for various file requests:

Here’s the resulting code.

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

    var htmlIndex = cleanPath.indexOf(".html");
    if (htmlIndex !== -1) {
        request.uri = cleanPath;
    }
    else {
        var lastChar = cleanPath[cleanPath.length - 1];
        if (lastChar == '/') {
            request.uri = cleanPath + "index.html";
        }
        else {
            if (cleanPath.lastIndexOf(".") < cleanPath.lastIndexOf("/")) {
                request.uri = cleanPath + "/index.html";
            }
        }
    }

    return request;
};

And that solves my current set of URL request issues on AWS. For now…

Lion gargoyle

Loading similar posts...   Loading links to posts on similar topics...

No Responses

Feel free to add a comment...

Leave a response

Note: some MarkDown is allowed, but HTML is not. Expand to show what's available.

  •  Emphasize with italics: surround word with underscores _emphasis_
  •  Emphasize strongly: surround word with double-asterisks **strong**
  •  Link: surround text with square brackets, url with parentheses [text](url)
  •  Inline code: surround text with backticks `IEnumerable`
  •  Unordered list: start each line with an asterisk, space * an item
  •  Ordered list: start each line with a digit, period, space 1. an item
  •  Insert code block: start each line with four spaces
  •  Insert blockquote: start each line with right-angle-bracket, space > Now is the time...
Preview of response