Cloudflare redirect rules with dynamic expressions

I wanted to ensure would redirect to - including any path - using Cloudflare.

I've solved this with page rules in the past, but this time I tried using a "redirect rule" instead.

Creating a redirect rule that only fires for hits to the (as opposed to hostname was easy. The harder part was figuring out how to assemble the URL.

I eventually found the clues I needed in this Cloudflare blog post. The trick is to assemble a "dynamic" URL redirect using the concat() function in the Cloudflare expression language, described here.

concat("", http.request.uri)

Here are the full configuration settings I used for my redirect rule:

Configuration screen for setting up a custom URL redirection rule. Custom filter expression is selected, indicating the rule will only apply to traffic matching the custom expression. When incoming requests match... Field: Hostname, Operator: Equals, Value: Expression Preview: ( eq ""). Then... URL redirect, Type: Dynamic, Expression: concat("", http.request.uri), Status code: 301 (permanent redirect). Preserve query string: This option is unchecked. Buttons: Cancel, Save as Draft, Deploy.

Created 2024-05-29T10:26:30+01:00 · Edit