Cloudfront function redirect behaves different for HTTPS request

28 Views Asked by At

The following code is a Cloudfront function designed to redirect a specific host to a new URL using a 301 Permanent Redirect.

async function handler(event) {
    const request = event.request
    const headers = request.headers
    const host = request.headers.host.value
    const uri = request.uri
    const newurl = 'https://www.newsite.com' + uri
  
    if (host === 'www.oldsite.com') {
        console.log(host)
        console.log('redirecting to www.newsite.com')
        const response = {
            statusCode: 301,
            statusDescription: 'Moved Permanently',
            headers:
                { "location": { "value": newurl } }
            }

        return response // return the altered request
    }
    return request // allow the request to continue to the origin
}

The code works as expected, and returns a 301 Moved Permanently response when navigating to http://www.oldsite.com/

However when accessing https://www.oldsite.com/ (note HTTPS) cloudfront returns a 301 response, without the status description.

Why?

Functionality in Firefox seems the same, but I thought the difference strange, and wondered where/what/why/how this was occurring.

Both responses redirect correctly to the new HTTPS site.

HTTPS request response

Screenshot of network tab response code for HTTPS request

HTTP request response

Screenshot of network tab response code for HTTP request

0

There are 0 best solutions below