Search code examples
amazon-web-servicesamazon-cloudfront

Does the Cache Policy in AWS Cloudfront allow for Wildcards in the query string?


I want to configure the Cloudfront Cache Policy as "Include all query strings except...". The exceptions should include various tracking query strings, such as the utm_* parameters. Unfortunately the list of query strings to exclude is limited to 10 entries. So I wondered if I can use wildcards for the utm parameters like this:

AWS Cloudfront Cache Policy query strings


Solution

  • I have been trying to solve this exact problem. Unfortunately, cache policies do not support the use of wildcards.

    In many scenarios, the utm_* query parameters are not needed on the backend server, but only by javascript running in the browser. If this describes your situation, you could consider a CloudFront viewer request function that simply drops all of the utm_* query parameters.

    Because the viewer request function is executed before the cache key is computed, the offending utm_* query parameters will not be present in that key. However, they remain in the browser for javascript code to use as needed.

    You can also take advantage of this hook to do further cache optimizations, such as sorting the remaining query parameters. (This is valuable because the same parameters in a different order result in a different cache key.)

    Your friendly neighborhood AI can generate a good starting point for code. Note that if you drop query parameters, your code must rebuild and properly encode the the query string and modify the request URI appropriately.

    I realize this solution doesn't cover all use cases; the lack of wildcards and the limited number of keys you can list are pretty restrictive.