Lambda@edge redirect gets in a redirect loop
up vote
0
down vote
favorite
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:
- How to correct the "too many redirects error"?
- 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
add a comment |
up vote
0
down vote
favorite
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:
- How to correct the "too many redirects error"?
- 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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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:
- How to correct the "too many redirects error"?
- 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
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:
- How to correct the "too many redirects error"?
- 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
amazon-s3 amazon-cloudfront aws-lambda-edge
edited Nov 19 at 19:53
asked Nov 19 at 13:04
user1305579
278
278
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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.
Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?
– user1305579
Nov 19 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 at 19:47
I have made an edit to the original question. Thank you for all the help.
– user1305579
Nov 19 at 19:54
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.
Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?
– user1305579
Nov 19 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 at 19:47
I have made an edit to the original question. Thank you for all the help.
– user1305579
Nov 19 at 19:54
add a comment |
up vote
0
down vote
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.
Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?
– user1305579
Nov 19 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 at 19:47
I have made an edit to the original question. Thank you for all the help.
– user1305579
Nov 19 at 19:54
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 19 at 16:07
Michael - sqlbot
86.2k11127189
86.2k11127189
Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?
– user1305579
Nov 19 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 at 19:47
I have made an edit to the original question. Thank you for all the help.
– user1305579
Nov 19 at 19:54
add a comment |
Will this redirect users where countryCode is not TR always to the index-en.html no matter what page they request?
– user1305579
Nov 19 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 at 19:47
I have made an edit to the original question. Thank you for all the help.
– user1305579
Nov 19 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 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 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 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 at 19:47
I have made an edit to the original question. Thank you for all the help.
– user1305579
Nov 19 at 19:54
I have made an edit to the original question. Thank you for all the help.
– user1305579
Nov 19 at 19:54
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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