Only redirect home route which may include query params

We’re currently redirecting our home route without query params, which is working fine. We want to include query params with the home route to send to the worker script to redirect.

I tried the following but creation failed, which makes sense since the docs state: “Route patterns may not contain infix wildcards or query parameters.”

mysite.com/?offer* => creation failed: Code 10022: Route pattern should not have query parameters mysite.com/?offer* 
mysite.com?offer* => creation failed:  Code 10022: Route pattern should not have query parameters mysite.com?offer*

Then I tried the following, which still failed since even the question mark with no query params in considered invalid, contrary to community post https://community.cloudflare.com/t/how-to-forward-url-and-preserve-query-params/255052.

mysite.com/?* => creation failed: Code 10022: Route pattern should not have query parameters mysite.com/?* 
mysite.com?* => creation failed: Code 10022: Route pattern should not have query parameters mysite.com?*

The challenge is that “Route pattern matching considers the entire request URL, including the query parameter string.”, but route patterns can not contain query parameters. Unless I’m missing something, this seems like quite a limitation! We don’t want to send every page on our site through the worker, when we only want to redirect the home page.

Any assistance appreciated!

I filed a support ticket and was informed this is a feature limitation with the current functionality. A possible suggested solution is to send everything to the worker to perform necessary redirects, but this is less than ideal. For starters, this would introduce a redirect into every API and page request.

From the docs, there are negation rules:

A route can be specified without being associated with a Worker. This will act to negate any less specific patterns. For example, consider this pair of route patterns, one with a Workers script and one without:

*example.com/images/cat.png -> <no script>
*example.com/images/*       -> worker-script

Based on this, a possible workaround would be to create a list 26 routes to be negated, one for each letter. For example, /a*, /b*… could all be negated. Then the remaining ‘?*’ would be sent to the worker as intended. At least I think this could work, but not yet clear how to accomplish this with the Wrangler config.

The solution to negate routes could also be a possibility and will look into that as well.

That post had nothing to do with matching query parameters. It was just to redirect a URL and carry the query strings over to the target.

Route matching here is quite picky, and I bet it’s for very good reason. Maybe someday regex will be more available so you wouldn’t need 26 negation rules.

1 Like

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