<p>CloudFront does not actually have this feature. You can redirect the root object <code>https://www.example.com/</code> to <code>https://www.example.com/index.html</code> by setting <code>DefaultRootObject</code>, but there's nothing for subdirectories such as <code>https://www.example.com/posts/</code>.</p>
<p>However, you can run arbitrary code on CloudFront using Lambda@Edge or CloudFront Functions, so it's possible for us to add this feature ourselves. With the &quot;origin request&quot; trigger we can rewrite the request to S3, changing <code>/posts/</code> to <code>/posts/index.html</code>. <a href="https://github.com/CloudUnder/lambda-edge-nice-urls" rel="nofollow noreferrer">https://github.com/CloudUnder/lambda-edge-nice-urls</a> has some good handling of paths that works well with Hugo. The Amazon documentation has an example of how you can do the same thing with <a href="https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-add-index.html" rel="nofollow noreferrer">CloudFront Functions</a>.</p>
<p>Another option is to enable <code>uglyurls</code> in your config so that all links within you site have <code>index.html</code> hardcoded in to them.</p>

CC BY-SA 4.0
2019-07-01T08:49:54.077Z
It is a nice idea, but as @Alexandre stated above, this answer solved the problem https://stackoverflow.com/questions/49082709/redirect-to-index-html-for-s3-subfolder
workaround
CC BY-SA 4.0
2021-07-23T16:48:14.063Z
this is really pain in the neck. a intuitive feature needs Lambda edge !?
Abdennour TOUMI
CC BY-SA 4.0
2019-06-29T20:30:47.800Z
2021-11-12T05:30:57.137Z
2021-11-12T05:30:57.137Z
user2640621
<p>For CFN (CloudFormation) users:</p>
<p>Look for</p>
<pre><code>    Type: AWS::CloudFront::Distribution
</code></pre>
<p>Replace</p>
<pre><code>        Origins:
          - DomainName: example.s3-website-ap-southeast-2.amazonaws.com
            Id: example
            S3OriginConfig:
              OriginAccessIdentity: &quot;&quot;
            OriginAccessControlId: !GetAtt CloudFrontOriginAccessControl.Id
</code></pre>
<p>with</p>
<pre><code>        Origins:
          - DomainName: example.s3-website-ap-southeast-2.amazonaws.com
            Id: example
            CustomOriginConfig:
              OriginProtocolPolicy: http-only
</code></pre>
<p>Also, you need to open your bucket read permission:</p>
<pre><code>  BucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: example
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal: &quot;*&quot;
            Action: s3:GetObject
            Resource: arn:aws:s3:::example/*
</code></pre>
<p><a href="https://i.sstatic.net/GPxibtkQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GPxibtkQ.png" alt="enter image description here" /></a>
<a href="https://i.sstatic.net/xFdyjbMi.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xFdyjbMi.png" alt="enter image description here" /></a></p>
<p>Official documents:</p>
<p><a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-origin.html#cfn-cloudfront-distribution-origin-customoriginconfig" rel="nofollow noreferrer">https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-origin.html#cfn-cloudfront-distribution-origin-customoriginconfig</a></p>
<p><a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteAccessPermissionsReqd.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteAccessPermissionsReqd.html</a></p>

CC BY-SA 4.0
2024-05-29T14:30:26.293Z
This is also answered here: https://stackoverflow.com/questions/49082709/redirect-to-index-html-for-s3-subfolder/78550206#78550206
Teemo
CC BY-SA 4.0
2024-05-29T14:29:23.133Z
2024-05-29T14:29:23.133Z
Teemo
<p>I have uploaded my HUGO static site into AWS S3 Bucket. I have also added the domain in cloudrfront.</p>

<p>The home index.html page works fine, but I cannot access my resources.
For example <code>https://www.example.com</code> works fine, but I cannot access <code>https://www.example.com/posts/</code>. I have to redirect links to the html file every time.</p>

<p>On localhost <code>https://www.example.com/posts/</code> displays automatically the page ( without redirecting <code>https://www.example.com/posts/</code> to <code>https://www.example.com/posts/index.html</code>.</p>

<p>Is there some property in cloudfront or Amazon S3 to replicate the behaviour of localhost and not have to change the links of the site?</p>

CC BY-SA 4.0
2019-06-28T13:48:16.007Z
Please have a look at [this answer](https://stackoverflow.com/questions/49082709/redirect-to-index-html-for-s3-subfolder) and let us know if it answers your question.
Alexandre
<amazon-s3><go><amazon-cloudfront>
Amazon S3 bucket does not redirect folder URLs to index.html
workaround
# Amazon S3 bucket does not redirect folder URLs to index.html

(Asked by: username_0 on 2019-06-27)

I have uploaded my HUGO static site into AWS S3 Bucket. I have also added the domain in cloudrfront.

The home index.html page works fine, but I cannot access my resources.
For example `https://www.example.com` works fine, but I cannot access `https://www.example.com/posts/`. I have to redirect links to the html file every time.

On localhost `https://www.example.com/posts/` displays automatically the page ( without redirecting `https://www.example.com/posts/` to `https://www.example.com/posts/index.html`.

Is there some property in cloudfront or Amazon S3 to replicate the behaviour of localhost and not have to change the links of the site?

**Tags:** amazon-s3, go, amazon-cloudfront

> **Comments:**
> **username_1**: Please have a look at [this answer](https://stackoverflow.com/questions/49082709/redirect-to-index-html-for-s3-subfolder) and let us know if it answers your question.  

---

## 2 answers

### Answer by username_2 on 2019-06-29. Score: 4

CloudFront does not actually have this feature. You can redirect the root object `https://www.example.com/` to `https://www.example.com/index.html` by setting `DefaultRootObject`, but there's nothing for subdirectories such as `https://www.example.com/posts/`.

However, you can run arbitrary code on CloudFront using Lambda@Edge or CloudFront Functions, so it's possible for us to add this feature ourselves. With the "origin request" trigger we can rewrite the request to S3, changing `/posts/` to `/posts/index.html`. <https://github.com/CloudUnder/lambda-edge-nice-urls> has some good handling of paths that works well with Hugo. The Amazon documentation has an example of how you can do the same thing with [CloudFront Functions](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-add-index.html).

Another option is to enable `uglyurls` in your config so that all links within you site have `index.html` hardcoded in to them.

> **Comments:**
> **username_0**: It is a nice idea, but as @username_1 stated above, this answer solved the problem https://stackoverflow.com/questions/49082709/redirect-to-index-html-for-s3-subfolder  
> **username_3**: this is really pain in the neck. a intuitive feature needs Lambda edge !?  

---

### Answer by username_4 on 2024-05-29. Score: 0

For CFN (CloudFormation) users:
Look for
    Type: AWS::CloudFront::Distribution

Replace
        Origins:
          - DomainName: example.s3-website-ap-southeast-2.amazonaws.com
            Id: example
            S3OriginConfig:
              OriginAccessIdentity: ""
            OriginAccessControlId: !GetAtt CloudFrontOriginAccessControl.Id

with
        Origins:
          - DomainName: example.s3-website-ap-southeast-2.amazonaws.com
            Id: example
            CustomOriginConfig:
              OriginProtocolPolicy: http-only

Also, you need to open your bucket read permission:
  BucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: example
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal: "*"
            Action: s3:GetObject
            Resource: arn:aws:s3:::example/*



Official documents:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-origin.html#cfn-cloudfront-distribution-origin-customoriginconfig
https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteAccessPermissionsReqd.html

> **Comments:**
> **username_4**: This is also answered here: https://stackoverflow.com/questions/49082709/redirect-to-index-html-for-s3-subfolder/78550206#78550206  

---

