> For the complete documentation index, see [llms.txt](https://tricks.bodik.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tricks.bodik.tech/infrastructure/aws/cloudfront/how-to-make-redirect-using-lambda-edge.md).

# How to make redirect using Lambda\@Edge?

This instruction will help you to make 301 redirect using CloudFront(CF) and Lambda\@Edge.

```javascript
exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const uri = request.uri;

    if (uri === "/some-url-you-want-to-redirect/") {
        const response = {
            status: '301',
            statusDescription: 'Permanently Moved',
            headers: {
                location: [{
                    key: 'Location',
                    value: 'https://site.com/url-for-redirection'
                }]
        }}
        callback(null, response);
    } else {
        callback(null, request)
    }
};
```

1. Create the function using Lambda Functions of AWS\
   ![](https://2605693744-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeAFt3tVeOuZFI31fb1yz%2Fuploads%2Fdqy2l21SLooqZjuNosk5%2Fimage.png?alt=media\&token=9f765bcd-65e7-46cd-ba07-9e7c1884f02a)
2. Deploy it on Lambda\@Edge\
   ![](https://2605693744-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeAFt3tVeOuZFI31fb1yz%2Fuploads%2FBHJRyhZMItVQI8zzOdCD%2Fimage.png?alt=media\&token=7cda1bee-76a6-4cf9-a061-890281e15bf4)
3. Attach it to Viewer Request on needed "Behavior" on CF.\
   ![](https://2605693744-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeAFt3tVeOuZFI31fb1yz%2Fuploads%2FgL1g4c54is2q4dznlbZS%2Fimage.png?alt=media\&token=92906c91-48cb-4a1f-b8c0-9ad7b6895396)\
   ![](https://2605693744-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeAFt3tVeOuZFI31fb1yz%2Fuploads%2F34ok5wK0RgJzaFlz6gZT%2Fimage.png?alt=media\&token=bba92828-5b19-4180-8cf3-53898b6f7eb5)
