Redirect /something.txt path to 404 status code

0

Hello everyone,

I would like to redirect existing page example.com/something.txt to give the result of status 404 page.

I have CloudFront distribution and two origins that point to S3 bucket and Application LoadBalancer.

Any suggestions how to achieve this?

asked 3 months ago156 views
2 Answers
1

Hi

Just a hint, it's written by hand so there could be a typos or logic errors, CloudFront Function attached to CloudFront distribution as Function associations / Viewer request:

 function handler(event) {
          var request = event.request;
          var uri = request.uri;
          
          if (uri.endsWith('/something.txt')) {
              statusCode: 404,
              statusDescription: "Not Found",
          } 
          return request;
}

Thanks,

profile picture
answered 3 months ago
profile pictureAWS
EXPERT
kentrad
reviewed 3 months ago
0

Hello everyone,

I wanted to provide the solution for this problem.

I created a Lambda Edge function (this needs to be in us-east-1):

``export async function handler(event) { const request = event.Records[0].cf.request;

// Check if request path is /something.txt
if (request.uri === '/something.txt') {
    // Return a 404 response
    return {
        status: '404',
        body: 'Not found',
        statusDescription: 'Not Found'
    };
}

// If request path is not /something.txt, pass through to the origin
return request;

};``

Then return back to CloudFront distribution and in the Behaviour part add path for /something.txt.

Inside of configuration for /something.txt in the Function Association add Origin request for your version of Lambda and arn of Lambda.

Hopefully this will be helpful for someone.

answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions