Ignore Query String for Dynamic Extensionless HTML Pages

Visitors come to pages on the website via ads that cause dynamic query string parameters to be used, for example:

https://www.site.com/section/category/subcategory/some-page?utm_source=google&gclid=34423523

I want to ignore that query string parameter so when the user lands on the site they’ll use the cached version of the page that would be served up via:

https://www.site.com/section/category/subcategory/some-page

Otherwise, all users that come in via these sorts of query strings get an uncached page.

I thought I had a solution, but then I read this: Understand Cloudflare Caching Level – Cloudflare Help Center

It says:

Ignore Query String only disregards the query string for static file extensions. For example, Cloudflare serves the style.css resource to requests for either style.css?this or style.css?that

Since my pages have no extension, it seems I can’t accomplish the sort of caching I’m looking for on the pro plan.

One workaround I really don’t want to do would be to give each page a static extension, such as “.css” or “.doc” (I could probably do this with some server-side routing, but it wouldn’t be pretty and may impact SEO at least initially).

Another option would be to upgrade from pro to enterprise (to create custom cache keys), but I suspect the client will not be a fan of the added cost (it’s apparently so expensive they don’t even list the cost). I also don’t know if enterprise would have a similar limitation I might run into.

Another option potentially to be to use a Cloudflare worker to remove the query strings at the edge, but I’m unsure if that’s viable as I haven’t tried it yet.

Are there any other options you can think of, or am I stuck with one of these workarounds?

If you are fine with actually removing the Query Strings, then you might want to consider the new Transform Rules

2 Likes

That might just work (so long as I can skip the rewrite when certain special query strings are present)! I’ll give it a shot and let you know how it goes.

1 Like

It worked!

I found that an error occurs when I try to use a regex, so had to work around that.

Here’s what I ended up with:

I basically had to approximate matching the URLs I was looking to rewrite, and added some rules to avoid matching based on query strings that cause modifications to the HTML. It matches a few more URLs than I’d like, but I think it should work out fine.

Thanks again for recommending this solution! Much easier than the other workarounds I was considering.

2 Likes

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