Lambda@edge redirect gets in a redirect loop












0















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.



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.



For this I have added a lambda@edge function with the following code:



'use strict';

/* This is an origin request function */
exports.handler = (event, context, callback) => {
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);
};


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.



However I get a "too many redirect error".



I have 2 questions:




  1. How to correct the "too many redirects error"?

  2. 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.


Appreciate help.
Thanks.










share|improve this question





























    0















    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.



    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.



    For this I have added a lambda@edge function with the following code:



    'use strict';

    /* This is an origin request function */
    exports.handler = (event, context, callback) => {
    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);
    };


    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.



    However I get a "too many redirect error".



    I have 2 questions:




    1. How to correct the "too many redirects error"?

    2. 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.


    Appreciate help.
    Thanks.










    share|improve this question



























      0












      0








      0








      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.



      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.



      For this I have added a lambda@edge function with the following code:



      'use strict';

      /* This is an origin request function */
      exports.handler = (event, context, callback) => {
      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);
      };


      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.



      However I get a "too many redirect error".



      I have 2 questions:




      1. How to correct the "too many redirects error"?

      2. 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.


      Appreciate help.
      Thanks.










      share|improve this question
















      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.



      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.



      For this I have added a lambda@edge function with the following code:



      'use strict';

      /* This is an origin request function */
      exports.handler = (event, context, callback) => {
      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);
      };


      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.



      However I get a "too many redirect error".



      I have 2 questions:




      1. How to correct the "too many redirects error"?

      2. 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.


      Appreciate help.
      Thanks.







      amazon-s3 amazon-cloudfront aws-lambda-edge






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 19:53







      user1305579

















      asked Nov 19 '18 at 13:04









      user1305579user1305579

      398




      398
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You are creating a redirect loop because you are sending the viewer back to the same site, same page, no matter what the results of your test.



          if (countryCode === 'TR') {
          return callback(null, request);
          } else {
          ...


          callback(null,request) tells CloudFront to continue processing the request -- not generate a response. Using return before the callback causes the rest of the trigger code not to run.






          share|improve this answer
























          • Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?

            – user1305579
            Nov 19 '18 at 19:28











          • You can't redirect users "no matter what page they request" unless you first verify that they did not request the page you're planning to redirect them to -- that is why you have a redirect loop. It was not clear from your question that all requests should go to a single page. You may need to clarify your question with an edit, describing exactly what behavior is expected under varying conditions.

            – Michael - sqlbot
            Nov 19 '18 at 19:47











          • I have made an edit to the original question. Thank you for all the help.

            – user1305579
            Nov 19 '18 at 19:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375285%2flambdaedge-redirect-gets-in-a-redirect-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You are creating a redirect loop because you are sending the viewer back to the same site, same page, no matter what the results of your test.



          if (countryCode === 'TR') {
          return callback(null, request);
          } else {
          ...


          callback(null,request) tells CloudFront to continue processing the request -- not generate a response. Using return before the callback causes the rest of the trigger code not to run.






          share|improve this answer
























          • Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?

            – user1305579
            Nov 19 '18 at 19:28











          • You can't redirect users "no matter what page they request" unless you first verify that they did not request the page you're planning to redirect them to -- that is why you have a redirect loop. It was not clear from your question that all requests should go to a single page. You may need to clarify your question with an edit, describing exactly what behavior is expected under varying conditions.

            – Michael - sqlbot
            Nov 19 '18 at 19:47











          • I have made an edit to the original question. Thank you for all the help.

            – user1305579
            Nov 19 '18 at 19:54
















          0














          You are creating a redirect loop because you are sending the viewer back to the same site, same page, no matter what the results of your test.



          if (countryCode === 'TR') {
          return callback(null, request);
          } else {
          ...


          callback(null,request) tells CloudFront to continue processing the request -- not generate a response. Using return before the callback causes the rest of the trigger code not to run.






          share|improve this answer
























          • Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?

            – user1305579
            Nov 19 '18 at 19:28











          • You can't redirect users "no matter what page they request" unless you first verify that they did not request the page you're planning to redirect them to -- that is why you have a redirect loop. It was not clear from your question that all requests should go to a single page. You may need to clarify your question with an edit, describing exactly what behavior is expected under varying conditions.

            – Michael - sqlbot
            Nov 19 '18 at 19:47











          • I have made an edit to the original question. Thank you for all the help.

            – user1305579
            Nov 19 '18 at 19:54














          0












          0








          0







          You are creating a redirect loop because you are sending the viewer back to the same site, same page, no matter what the results of your test.



          if (countryCode === 'TR') {
          return callback(null, request);
          } else {
          ...


          callback(null,request) tells CloudFront to continue processing the request -- not generate a response. Using return before the callback causes the rest of the trigger code not to run.






          share|improve this answer













          You are creating a redirect loop because you are sending the viewer back to the same site, same page, no matter what the results of your test.



          if (countryCode === 'TR') {
          return callback(null, request);
          } else {
          ...


          callback(null,request) tells CloudFront to continue processing the request -- not generate a response. Using return before the callback causes the rest of the trigger code not to run.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 16:07









          Michael - sqlbotMichael - sqlbot

          91.1k12133194




          91.1k12133194













          • Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?

            – user1305579
            Nov 19 '18 at 19:28











          • You can't redirect users "no matter what page they request" unless you first verify that they did not request the page you're planning to redirect them to -- that is why you have a redirect loop. It was not clear from your question that all requests should go to a single page. You may need to clarify your question with an edit, describing exactly what behavior is expected under varying conditions.

            – Michael - sqlbot
            Nov 19 '18 at 19:47











          • I have made an edit to the original question. Thank you for all the help.

            – user1305579
            Nov 19 '18 at 19:54



















          • Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?

            – user1305579
            Nov 19 '18 at 19:28











          • You can't redirect users "no matter what page they request" unless you first verify that they did not request the page you're planning to redirect them to -- that is why you have a redirect loop. It was not clear from your question that all requests should go to a single page. You may need to clarify your question with an edit, describing exactly what behavior is expected under varying conditions.

            – Michael - sqlbot
            Nov 19 '18 at 19:47











          • I have made an edit to the original question. Thank you for all the help.

            – user1305579
            Nov 19 '18 at 19:54

















          Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?

          – user1305579
          Nov 19 '18 at 19:28





          Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?

          – user1305579
          Nov 19 '18 at 19:28













          You can't redirect users "no matter what page they request" unless you first verify that they did not request the page you're planning to redirect them to -- that is why you have a redirect loop. It was not clear from your question that all requests should go to a single page. You may need to clarify your question with an edit, describing exactly what behavior is expected under varying conditions.

          – Michael - sqlbot
          Nov 19 '18 at 19:47





          You can't redirect users "no matter what page they request" unless you first verify that they did not request the page you're planning to redirect them to -- that is why you have a redirect loop. It was not clear from your question that all requests should go to a single page. You may need to clarify your question with an edit, describing exactly what behavior is expected under varying conditions.

          – Michael - sqlbot
          Nov 19 '18 at 19:47













          I have made an edit to the original question. Thank you for all the help.

          – user1305579
          Nov 19 '18 at 19:54





          I have made an edit to the original question. Thank you for all the help.

          – user1305579
          Nov 19 '18 at 19:54




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375285%2flambdaedge-redirect-gets-in-a-redirect-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini