Hi,
I’d like to use dynamic single redirects (not workers) in order to transform URLs like https://www.example.com/foo/bar
into https://private.example.com/?param=%2Ffoo%2Fbar
(with or without the full URI).
The issue is there is no url_encode function available, while url_decode is fully available.
https://developers.cloudflare.com/ruleset-engine/rules-language/functions/#function-url_decode
I’d like to encode path, and query parameters.
Do you know a why “url_encode” is not available? And how I could bypass this?
Perhaps we could use a long list of nested “regex_replace” functions, character by character, but I hope there is a better way.
Thanks in advance!
This cannot be achieved with Single Redirects currently. Fortunately, it is trivial to do with Cloudflare Workers:
export default {
async fetch(request) {
const requestUrl = new URL(request.url)
const redirectUrl = new URL('https://private.example.com/')
// Path only
redirectUrl.searchParams.set('path', requestUrl.pathname)
// Path and query
redirectUrl.searchParams.set('uri', requestUrl.pathname + requestUrl.search)
// Full URI
redirectUrl.searchParams.set('full_uri', requestUrl.toString())
return Response.redirect(redirectUrl, 302)
}
}
$ curl -i 'https://example.com/example/path?example=query'
HTTP/2 302
date: Fri, 09 Jun 2023 09:29:30 GMT
content-length: 0
location: https://private.example.com/?path=%2Fexample%2Fpath&uri=%2Fexample%2Fpath%3Fexample%3Dquery&full_uri=https%3A%2F%2Fexample.com%2Fexample%2Fpath%3Fexample%3Dquery
server: cloudflare
cf-ray: 7d4859beff38be3d-CPH
https://private.example.com/
?path=%2Fexample%2Fpath
&uri=%2Fexample%2Fpath%3Fexample%3Dquery
&full_uri=https%3A%2F%2Fexample.com%2Fexample%2Fpath%3Fexample%3Dquery