title: Lambda@edge redirect gets in a redirect loop
body: <p>I have a static website on aws s3. Have setup route 53 and cloud front and everything works smoothly. s3 Bucket is setup to serve index.html as index document.</p>
  
  <p>Now I have added another file called index-en.html that should be served when the request country is any other country and not my home country.</p>
  
  <p>For this I have added a lambda@edge function with the following code:</p>
  
  <pre><code>'use strict';
  
  /* This is an origin request function */
  exports.handler = (event, context, callback) =&gt; {
      const request = event.Records[0].cf.request;
      const headers = request.headers;
  
      /*
       * Based on the value of the CloudFront-Viewer-Country header, generate an
       * HTTP status code 302 (Redirect) response, and return a country-specific
       * URL in the Location header.
       * NOTE: 1. You must configure your distribution to cache based on the
       *          CloudFront-Viewer-Country header. For more information, see
       *          http://docs.aws.amazon.com/console/cloudfront/cache-on-selected-headers
       *       2. CloudFront adds the CloudFront-Viewer-Country header after the viewer
       *          request event. To use this example, you must create a trigger for the
       *          origin request event.
       */
  
      let url = 'prochoice.com.tr';
      if (headers['cloudfront-viewer-country']) {
          const countryCode = headers['cloudfront-viewer-country'][0].value;
          if (countryCode === 'TR') {
              url = 'prochoice.com.tr';
          } else {
              url = 'prochoice.com.tr/index-en.html';
          }
      }
  
      const response = {
          status: '302',
          statusDescription: 'Found',
          headers: {
              location: [{
                  key: 'Location',
                  value: url,
              }],
          },
      };
      callback(null, response);
  };
  </code></pre>
  
  <p>I have also edited cloud front behavior to whitelist Origin and Viewer-country headers and setup the cloudfront Viewer-Request event and lambda Function ARN relation.</p>
  
  <p>However I get a "too many redirect error".</p>
  
  <p>I have 2 questions:</p>
  
  <ol>
  <li>How to correct the "too many redirects error"?</li>
  <li>For viewers outside "TR" the default landing page should be index-en.html, from which 2 more pages in english are accessible via navigation menu. So when users request a specific page from page navigation they should be able to access those pages, when no page is requested the default landing page should be served.</li>
  </ol>
  
  <p>Appreciate help.
  Thanks. </p>