title: Cloudfront does not redirect properly to https and subdomain
body: <p>According to <a href="https://stackoverflow.com/a/48198816">this</a> post and <a href="https://simpleit.rocks/web/hostings/redirect-http-to-https-and-www-to-non-www-with-aws-s3-bucket-cloudfront-route-53-and-a-custom-domain/" rel="nofollow noreferrer">this</a> article, one should create a second cloudfront distribution in front of an S3 bucket which redirects to another bucket, which contains the static files.</p>
  
  <p>Asuuming the frontend bucket name is <code>www.example.com</code> and the redirect bucket is <code>example.com</code>. I have set up my AWS resources according to the following cloudformation template.</p>
  
  <p>However, some problems occur: If I hit example.com it does not redirect to www.example.com, only when I have clicked another link on the site. In addition, it does not load the favicon for example. <a href="http://example.com" rel="nofollow noreferrer">http://example.com</a> is redirect to <a href="https://example.com" rel="nofollow noreferrer">https://example.com</a>. <a href="http://www.example.com" rel="nofollow noreferrer">http://www.example.com</a> it does not redirect to https, the webiste is not found.</p>
  
  <p>What am I missing in my AWS settings?</p>
  
  <p><a href="https://simpleit.rocks/web/hostings/redirect-http-to-https-and-www-to-non-www-with-aws-s3-bucket-cloudfront-route-53-and-a-custom-domain/" rel="nofollow noreferrer">This</a> mentions to not set the Default Root Object property, which I did not. But maybe it is related somehow?</p>
  
  <pre><code>FrontendBucket:
        Type: AWS::S3::Bucket
        Properties:
          BucketName: ${self:custom.frontendBucketName}
          AccessControl: PublicRead
          WebsiteConfiguration:
            IndexDocument: index.html
            ErrorDocument: 404.html
      RedirectdBucket:
        Type: AWS::S3::Bucket
        Properties:
          BucketName: ${self:custom.redirectBucketName}
          AccessControl: PublicRead
          WebsiteConfiguration:
            RedirectAllRequestsTo:
              HostName: ${self:custom.frontendBucketName}
              Protocol: https
  
  WebAppCloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Origins:
        - DomainName: ${self:custom.frontendBucketName}.s3-website.${self:provider.region}.amazonaws.com
          Id: Frontend
          CustomOriginConfig:
            HTTPPort: 80
            HTTPSPort: 443
            OriginProtocolPolicy: http-only
        Enabled: 'true'
        Aliases:
        - ${self:custom.frontendBucketName}
        CustomErrorResponses:
        - ErrorCode: 404
          ResponseCode: 200
          ResponsePagePath: /index.html
        DefaultCacheBehavior:
          DefaultTTL: 31536000
          MaxTTL: 31536000
          MinTTL: 31536000
          AllowedMethods:
          - DELETE
          - GET
          - HEAD
          - OPTIONS
          - PATCH
          - POST
          - PUT
          TargetOriginId: Frontend
          ForwardedValues:
            QueryString: 'false'
            Cookies:
              Forward: none
          ViewerProtocolPolicy: redirect-to-https
          AcmCertificateArn: 'arn:aws:acm:us-east-1:xxxx:certificate/xxxx'
          SslSupportMethod: 'sni-only'
  
  RedirectCloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Origins:
          - DomainName: ${self:custom.redirectBucketName}.s3-website.${self:provider.region}.amazonaws.com
            Id: Redirect
            CustomOriginConfig:
              HTTPPort: 80
              HTTPSPort: 443
              OriginProtocolPolicy: http-only
        Enabled: 'true'
        Aliases:
        - {self:custom.redirectBucketName}
        DefaultCacheBehavior:
          DefaultTTL: 31536000
          MaxTTL: 31536000
          MinTTL: 31536000
          AllowedMethods:
          - DELETE
          - GET
          - HEAD
          - OPTIONS
          - PATCH
          - POST
          - PUT
          TargetOriginId: Redirect
          ForwardedValues:
            QueryString: 'false'
            Cookies:
              Forward: none
          ViewerProtocolPolicy: redirect-to-https
        ViewerCertificate:
          AcmCertificateArn: 'arn:aws:acm:us-east-1:xxxx:certificate/xxxx'
          SslSupportMethod: 'sni-only'
  
  
  DnsRecord:
    Type: "AWS::Route53::RecordSet"
    Properties:
      AliasTarget:
        DNSName:
          Fn::GetAtt:
            - WebAppCloudFrontDistribution
            - DomainName
        HostedZoneId: XXXXX
      HostedZoneId: XXXX
      Name: ${self:custom.frontendBucketName}
      Type: 'A'
  
  RedirectDnsRecord:
    Type: "AWS::Route53::RecordSet"
    Properties:
      AliasTarget:
        DNSName:
          Fn::GetAtt:
            - RedirectCloudFrontDistribution
            - DomainName
        HostedZoneId: XXXX
      HostedZoneId: XXXX
      Name: ${self:custom.redirectBucketName}
      Type: 'A'
  </code></pre>