Rewrite URL

We are getting a number of requests that are causing errors with our site. They all have &sa=U&&ved= at the end of the URL which come from Google. As this is not a valid URL for us we need to either rewrite or redirect so that they start with ?sa=U&ved= or we strip it all completely.

Tried a transform rule (http.request.full_uri contains “&sa=U&”) and then dynamic rewrite regex_replace(http.request.full_uri, “&sa=U&”, “?sa=U&”)

but doesnt seem to match when I test it.
Any ideas - Thx

Change http.request.full_uri to http.request.uri.query and ensure its in the Query section of the Rewrite rule, and its set to ‘Dynamic’.


So now I’ve got
(http.request.uri.query eq “&sa=U&”)

Query Rewrite to dynamic to - regex_replace(http.request.uri.query, “&sa=U&”, “?sa=U&”)

but still not matching… As there is no ? in the original request will it trigger a http.request.uri.query?

I’ve gone back to full_uri as I dont think it will match as uri.query without a ? in there.

Does the regex look right to replace the &sa= with ?sa=
Is there any escaping I need to do with CF?


That’s a tricky one.

With Transform Rules - Rewrite URL, you can only rewrite the path, the query, or both. The ? delimiter is not part of either the http.request.uri.path or the http.request.uri.query field. However, since the URL contains & where a ? should be, Cloudflare is treating what you consider your query string as part of the path element (http.request.uri.path = /&query...), while http.request.uri.query is left empty.

What I found that (kind of) works is this:

Rewrite Path (Dynamic): regex_replace(http.request.uri.path, "&.*", "")

Here we’re telling TR to replace

respectively with



Rewrite Query (Dynamic): regex_replace(http.request.uri, ".*&", "")

Since http.request.uri.path has already been modified, we take http.request.uri (/some/path/&some=query) to extract the query string, removing everything up to the first &.

I said it “kind of” worked because, strangely enough, in my tests it only worked when the path ends with a trailing slash immediately preceding the &. So it worked for

but not with

Your URL examples indicate your URLs do not end with a trailing slash. Perhaps another @MVP with experience in using regex with Transform Rules could suggest a better regex expression for this case.

But somehow a Redirect Rule with a regex_replace and Full URI worked without the trailing slash:

Type: Dynamic
Expression: regex_replace(http.request.full_uri, "/(.*)&", "/${1}?")

I hope it works for you. As with everything related to Regex, make sure you test, test, then test again.