Redirect Rules Cheat Sheet

Cloudflare Page Rules will soon be deprecated, as they are being replaced by a group of new Cloudflare Rules.

Redirect Rules (beta) helps you make all kinds of dynamic or static redirects.

Examples of some of the most requested Redirect Rules:

1 - Canonical redirect (naked domain to www.)

Redirect Rule details

When incoming requests match

Field: Hostname
Operator: equals
Value: example.com

Then

Type: Dynamic
Expression: concat("https://www.example.com", http.request.uri.path)
Status code: 301
Preserve query string: :heavy_check_mark:

2 - Canonical redirect (www. to naked domain)

Redirect Rule details

When incoming requests match

Field: Hostname
Operator: equals
Value: www.example.com

Then

Type: Dynamic
Expression: concat("https://example.com", http.request.uri.path)
Status code: 301
Preserve query string: :heavy_check_mark:

3 - Redirect from one domain to another, keeping requested path & query string

Redirect Rule details

When incoming requests match

Field: Hostname
Operator: equals
Value: example.com

Then

Type: Dynamic
Expression: concat("https://example.org", http.request.uri.path)
Status code: 301
Preserve query string: :heavy_check_mark:

4 - Redirect from one domain to the homepage of another domain

Redirect Rule details

When incoming requests match

Field: Hostname
Operator: contains [this operator will match both example.com and www.example.com]
Value: example.com

Then

Type: Static
URL: https://example.org/
Status code: 301
Preserve query string: :heavy_check_mark:

5 - Redirect a subdomain to a subdirectory

Redirect Rule details

When incoming requests match

Field: Hostname
Operator: equals
Value: subdomain.example.com

Then

Type: Dynamic
Expression: concat("https://www.example.com/subdirectory", http.request.uri.path)
Status code: 301
Preserve query string: :heavy_check_mark:

6 - Redirect subdirectory to subdomain

Redirect Rule details

When incoming requests match

Field: URI Path
Operator: starts with
Value: /subdirectory/

Then

Type: Dynamic
Expression: concat("https://subdomain.example.com", substring(http.request.uri.path, 13))
(* The number in the substring() function should be the number of characters in the subdirectory name + 1.)
Status code: 301
Preserve query string: :heavy_check_mark:

7 - Redirect from a specific path to another, same domain

Redirect Rule details

When incoming requests match

Field: URI Path
Operator: equals
Value: /contact-us/

Then

Type: Static
URL: /contacts/
Status code: 301
Preserve query string: :heavy_check_mark:

8 - Redirect requests with non-standard ports

Redirect Rule details

When incoming requests match
Expression (in Expression Editor): not (cf.edge.server_port in {80 443})

Then

Type: Dynamic
Expression: concat("https://", http.host, http.request.uri.path)
Status code: 303
Preserve query string: No.

Other Redirect Rules Examples From the Documentation:

Redirect users from some countries to country-specific subdomains
Single Redirects — Example rules · Cloudflare Rules docs

Remove locale (e.g. “en-US”, “fr-FR”) from URL path
Single Redirects — Example rules · Cloudflare Rules docs

14 Likes