Redirect if URI query string does not match numbers

What is the name of the domain?

nutripath.com.au

What is the issue you’re encountering

Unable to get the target URI to redirect unless the query edns with [0-9]{2}-[0-9]{6}

What steps have you taken to resolve the issue?

Have used rust regex testers and attempted to follow the CLoudFlare guides

What are the steps to reproduce the issue?

Try to reach https://form.nutripath.com.au/forms/pqsaliva/order_no=12345 ← should not work
Try to reach https://form.nutripath.com.au/forms/pqsaliva/order_no=abc123 ← should not work
Try to reach Invalid Link - NutriPATH Integrative and Functional Pathology Services. ← should not work
Try to reach Invalid Link - NutriPATH Integrative and Functional Pathology Services. ← should work

Expression string is currently as follows:

(http.request.full_uri wildcard r"https://form.nutripath.com.au*" and not http.request.uri.query contains “order_no=”) or (http.request.full_uri wildcard r"https://form.nutripath.com.au*" and http.request.full_uri eq “https://form.nutripath.com.au”) or (http.request.full_uri wildcard r"https://form.nutripath.com.au*" and not starts_with(http.request.uri.path, “/forms/pq”))

I am afraid using Regex, matches, would require a Business or Enterprise plan:

If you’re using a Pro plan, I guess using Snippets could be achievable as well.

However, it is possible using Workers as well on a Free plan as follows from below:
Worker code:

addEventListener('fetch', event => {
  const url = new URL(event.request.url);
  const orderNo = url.searchParams.get('order_no');
  
  // Regex to match the order_no format "XX-XXXXXX", 2 numbers followed by the dash then 6 numbers
  const regex = /^\d{2}-\d{6}$/;

  if (orderNo && regex.test(orderNo)) {
    // Redirect to the new URL if it matches the pattern
    const newUrl = `https://www.mywebsite.com/order/${orderNo}/`; // or what ever the new URL should contain those numbers or what?
    event.respondWith(Response.redirect(newUrl, 301));
  } else {
    // If it doesn't match, continue with the request
    event.respondWith(fetch(event.request));
  }
});

I’ve tested this on my Website and it does work.
Redirects me when I have 2 numbers followed by the dash then 6 numbers, rather matches e.g. order_no=00-123456 but not order_no=abc-123456 nor ab-123456 nor some other combination. If I have something different, it just proceeds forward with the request (or should throw some origin 404 depends how your environment and web application is configured with your web server).

You have to bound this Worker to your zone/domain and add your Worker route as https://form.nutripath.com.au/forms/pqsaliva/*. Then, in your Worker code change the part of the new URL to which it should redirect (mywebsite.com/order/...).

Or maybe from your topic title, vice-versa we need to fetch? :thinking: (then the if-else is just vice-versa in the above code, meaning else if it doesn’t match then respond and redirect with newUrl 301).

Helpful article:

1 Like

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