Cloudflare Docs
Rules
Rules
Visit Rules on GitHub
Set theme to dark (⇧+D)

Example rules

The following sections contain example single redirect rule configurations.

​​ Redirect visitors to the new URL of a page

This example static redirect for zone example.com will redirect visitors requesting the /contact-us/ page to the new page URL /contacts/.

When incoming requests match

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

If you are using the Expression Editor, enter the following expression:
http.request.uri.path eq "/contact-us/"

Then

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

For example, the redirect rule would perform the following redirects:

Request URLTarget URLStatus code
example.com/contact-us/example.com/contacts/301
example.com/contact-us/?state=TXexample.com/contacts/?state=TX301
example.com/team/(unchanged)n/a

​​ Redirect UK and France visitors to their specific subdomains

This example dynamic redirect for zone example.com will redirect United Kingdom and France visitors requesting the website’s root path (/) to their localized subdomains https://gb.example.com and https://fr.example.com, respectively.

When incoming requests match

Using the Expression Editor:
(ip.geoip.country eq "GB" or ip.geoip.country eq "FR") and http.request.uri.path eq "/"

Then

  • Type: Dynamic
  • Expression: lower(concat("https://", ip.geoip.country, ".example.com"))
  • Status code: 301

For example, the redirect rule would perform the following redirects:

Visitor countryRequest URLTarget URLStatus code
United Kingdomexample.comhttps://gb.example.com301
Franceexample.comhttps://fr.example.com301
United Statesexample.com(unchanged)n/a

​​ Remove locale information from URL path

This example dynamic redirect for zone example.com will redirect visitors from an old URL format that included the locale (for example, /en-us/<page_name>) to the new format /<page_name>.

When incoming requests match

  • Field: URI Path
  • Operator: matches regex
  • Value: ^/[A-Za-z]{2}-[A-Za-z]{2}/

If you are using the Expression Editor, enter the following expression:
http.request.uri.path matches "^/[A-Za-z]{2}-[A-Za-z]{2}/"

Then

  • Type: Dynamic
  • Expression: regex_replace(http.request.uri.path, "^/[A-Za-z]{2}-[A-Za-z]{2}/(.*)", "/${1}")
  • Status code: 301
  • Preserve query string: Enabled

The function regex_replace() allows you to extract parts of the URL using regular expressions’ capture groups. Create capture groups by putting part of the regular expression in parentheses. Then, reference a capture group using ${<num>} in the replacement string, where <num> is the number of the capture group.

For example, the redirect rule would perform the following redirects:

Request URLTarget URLStatus code
example.com/en-us/meet-our-teamexample.com/meet-our-team301
example.com/pt-BR/meet-our-teamexample.com/meet-our-team301
example.com/en-us/calendar?view=monthexample.com/calendar?view=month301
example.com/meet-our-team(unchanged)n/a
example.com/robots.txt(unchanged)n/a