3

I'm trying do something like below for HTTP 301 redirect, so that web users will redirect to different news pages.

    if ((request.uri == "/news") || (request.uri == "/news/") && (request.origin.domainName == "sub.mydomain.com")) {

        
        const redirectResponse = {
            status: '301',
            statusDescription: 'Moved Permanently',
            headers: {
                'location': [{
                    key: 'Location',
                    value: '/local-news/',
                }],
                'cache-control': [{
                    key: 'Cache-Control',
                    value: "max-age=3600"
                }],
            },
        };
        callback(null, redirectResponse);
   
  }

However, seems like this request.origin.domainName == "mydomain.com" part is not working in my function. Is this correct way to pick the domain name which client coming from?

I think this request.origin.domainName method will not work as Origin Object support only for Origin requests. Is there any possibility, that I can get the domain name which client coming from for the Viewer requests?

The reason I need this is, I've multiple domains, that users access the same CloudFront distribution. Hence, when user coming from different domains, user must be redirected to different news sites.

Can anyone support me on this?

hlesnt395
  • 603
  • 10
  • 30

1 Answers1

5

If you want to get distribution Domain Name

  const distributionDomain = event.Records[0].cf.config.distributionDomainName;

The more information You can find in AWS Documentation

Also, check

Lambda@Edge Example Functions

Doc

accessing origin URL from AWS lambda@edge

Also, try this way

'use strict';

exports.handler = (event, context, callback) => {
    const response = event.Records[0].cf.response;
    const request = event.Records[0].cf.request;
    const hostHeader = request.headers['host'][0].value;
    callback(null, response);
};

hostHeader should be the CNAME(domain name)

The more info here

CyberEternal
  • 2,259
  • 2
  • 12
  • 31
  • Thanks for the response @CyberEternal. `distributionDomainName` gives the CloudFront distribution domain. I actually want my custom domain (CNAME created to CloudFront address) which user access the website. – hlesnt395 Oct 22 '20 at 02:30
  • This is cool and it works like charm. Thank you so much. Would like to check with you, if you can help me to understand, can I do something like `https://test.mydomain.com/data/news*` > `https://test.mydomain.com/data/local-news$1` 301 redirections? – hlesnt395 Oct 22 '20 at 08:14
  • I think You can check the path parameters of the viewer request or origin request, and add the logic for the condition. – CyberEternal Oct 22 '20 at 13:09
  • If we talk about an Lambda@Edge handling origin origin requests, the `distributionDomainName` value is the distribution default domain (`whatever.cloudfront.net`), and the `request.headers['host'][0].value` value is the origin domain name. None of these is equal to the hostname that the client used to send the request to the distribution. – Finesse Dec 29 '21 at 05:00