Preserving Port Number When Redirecting

I’m using a redirect rule to remove the www. subdomain from URLs:

But when I add a port:

It redirects to:

Instead to:

How to preserve the port in this redirect rule?

Havent tried it personally, but you should be able to add the cf.edge.server_port field in your concat.
More on what that field is here:


I’ve tried this but I’m getting an error:


Any ideas on how to form it better?

I’ve not tried it, but probably needs this…



Correct! Beat me to it.
concat("https://", substring(, 4),":",to_string(cf.edge.server_port),http.request.uri.path) works great for me.

(Why?) - concat() joins fields of a string type. the port is returned as an integer so cant be used in the join. We can convert it to a string using the function to_string() which then lets us treat it as such.


It works for URIs with a port but for URIs without the port, it returns this: =>

My suggestion is to add more preservation options like “Preserve query string”. You can add “Preserve port” or maybe “Preserve URI path”. I know you can add these to the expression but it’s easier this way.

If thats a problem you can always adjust the expression, e.g. AND cf.edge.server_port ne 443 (again, havent tested this, you might need to tweak the syntax).

1 Like

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