aws

A CloudFront Function to remove a specific value at the beginning of an URL

lboix

Lucien Boix

Posted on January 25, 2023

A CloudFront Function to remove a specific value at the beginning of an URL

My usecase was :

  • the CDN was delivering images from a S3 bucket
  • the images URL pattern was https://URL/images/something.jpg
  • there was not an "images" folder at the root of the S3 bucket, images like something.jpg were directly there

So I used this function and associated it to the right Behavior ("Viewer request" option) of my CloundFront Distribution :

function handler(event) {

    var request = event.request;

    if (request.uri.startsWith("/images/")) {
        request.uri = request.uri.substring(7);
    }

    return request;
}
Enter fullscreen mode Exit fullscreen mode

Let me know if that helped you or if you have suggestion for improvements. Take care !

šŸ’– šŸ’Ŗ šŸ™… šŸš©
lboix
Lucien Boix

Posted on January 25, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related