Single Redirects - how to url_encode?


I’d like to use dynamic single redirects (not workers) in order to transform URLs like into (with or without the full URI).

The issue is there is no url_encode function available, while url_decode is fully available.
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('')

		// Path only
		redirectUrl.searchParams.set('path', requestUrl.pathname)

		// Path and query
		redirectUrl.searchParams.set('uri', requestUrl.pathname +

		// Full URI
		redirectUrl.searchParams.set('full_uri', requestUrl.toString())

		return Response.redirect(redirectUrl, 302)
$ curl -i ''
HTTP/2 302 
date: Fri, 09 Jun 2023 09:29:30 GMT
content-length: 0
server: cloudflare
cf-ray: 7d4859beff38be3d-CPH