Get cloudflare cache to ignore querystrings from cached html without Enterprise?

What is the name of the domain?

no domain

What is the error number?

No error

What is the error message?

No error

What is the issue you’re encountering

We’re wanting to cache all the html content from a php generated page. And we want to serve the cached version of the page no matter what the query strings are applied.

What steps have you taken to resolve the issue?

So we want example.com and example.com ?fbclid=123 to serve the same cached version of the page. I realise there is a setting in the cache rules section, but it is only for enterprise customers. Is there some way to do this without enterprise level account?

You can ignore query strings in the cache settings, but it is a zone wide option:

Alternatively, if you only want this for a specific file, you could use a Worker to intercept that route and serve that file from cache.

Edit: Or you could use a Redirect Rule that essentially removes any query string.

2 Likes

Using below Page Rule you could redirect Facebook shared links to a cached HTML page, which would also fetch any param=value after fbclid.

Is your admin area separated from normal viewer/visitor? :thinking:
Do you use a cookie?

1 Like

Thank you very much for your suggestions! I did look at the cache settings but it appears it only works for static files like css/js? Caching levels · Cloudflare Cache (CDN) docs

I haven’t tested it but I assume that means it won’t work for cached html?

A redirect won’t work because we still need the query strings, we just don’t want to have to go back to the server for the html for each different query string value, because this means the cache is bypassed for all users with a different click id string.

We also looked at the worker option but we couldn’t work out how to make it work. We put the html content of the page into the worker, but the route setting in the worker doesn’t allow query strings.

Is there some way we can deploy a worker to just catch a single path with specific query strings and call the cached html resource instead of going back to the server?

Thanks very much for this, but am I right that this will just remove the query strings altogether? We’re needing the querystrings but just don’t want to have to go back to the server for each request with a different querystring.

Could you re-write your Worker so it’ll match URLs which contain query? :thinking:
So you could bound to Worker Route such as example.com/something/* if that should catch and check all the URLs after something/ with a query parameter.

I have to say, using Worker might be costly in such case since we cannot bound a Worker Route to Query parameter, so it’ll be like checking each URL from each search engine trying to visit, crawl, index, bot, attack … might not be great case.

Or just for the style.css? :thinking:

May I ask if you’re using Free or paid plan type?

So in an example, you’d want to return style.css which is cached, instead of style.css?param=value which would always go to the origin for style.css right?

1 Like

We are using a paid plan.
Yes your example is correct. We want to return the cached version of example .com instead of going to the origin for example .com?param=value.

I have to try on a Pro plan at least using Snippets, maybe something could be done.

1 Like

You could definitely do this as a worker as we had something similar working.
Basis is to explode the list of querystring parameters, strip out the ones you don’t want and then create a new request to the origin using just the ones you do want and return that to the requester.
Because workers sits in front of the cache the request will load from cache without touching the origin.
You do incur the cost of 1 worker execution per page load for anything on the path in question but that may still be way less than the computation cost of generating at the origin.

2 Likes

Yes, it seems you’re right and ignoring the query string for other file types is only available on Enterprise. That’s a bit annoying.

In that case, I think using Snippets as explained by @fritex might be a good idea.

Here’s an example from the Cloudflare docs:

1 Like

Thanks for the suggestion of Snippets @fritex and @Laudian. I have tried using snippets and they work beautifully! From our php we output a static html file for the page and using the snippet we check if a specific querystring exists on a specific uri path, and if it does the snippet runs and instead of going back to the origin, it loads the cached static file instead. There are no limits/cost on how many times a snippet can run.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.