Ad

AWS S3 Static Website Accessible At /page.html And Not /page

- 1 answer

I am new to AWS and using S3 to host my static website! I have an html file, page.html and it should be acessible at mydomain.com/page. However, it is only accessible at mydomain.com/page.html :(. How can I fix this?

Ad

Answer

Unfortunately if you want this to work there are a couple of solutions that you can try.

For the first by creating an index document in a subfolder i.e. /page/index.html you would be able to access your site on /page/. This would work well if you're using a hierarchy.

However, if you have many many pages then you will need to use another solution that can rewrite the client site URL to your .html file.

For this you can use CloudFront with a [email protected] function to perform this in the Origin Request stage. This function might look similar to the below.

import json
import os
 
def lambda_handler(event, context):
    request = event['Records'][0]['cf']['request']
    
    filepath, extension = os.path.splitext(request['uri'])
    
    if not extension:
        new_uri = request['uri'] + '.html'
        request['uri'] = new_uri
    
    return request

By doing this your users will see a nice clean path but when it goes to your origin it will append the extension.

Ad
source: stackoverflow.com
Ad