Cloudflare Docs
Rules
Rules
Edit this page
Give us feedback
Set theme to dark (⇧+D)

Example rules

The following sections contain example single redirect rule configurations.

​​ Redirect visitors to the new URL of a specific 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 all requests to a different hostname

This example dynamic redirect will redirect all requests for smallshop.example.com to a different hostname using HTTPS, keeping the original path and query string.

When incoming requests match

  • Field: Hostname
  • Operator: equals
  • Value: smallshop.example.com

If you are using the Expression Editor, enter the following expression:
(http.host eq "smallshop.example.com")

Then

  • Type: Dynamic
  • Expression: concat("https://globalstore.example.net", http.request.uri.path)
  • Status code: 301
  • Preserve query string: Enabled

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

Request URLTarget URLStatus code
http://smallshop.example.com/https://globalstore.example.net/301
http://smallshop.example.com/admin/?logged_out=truehttps://globalstore.example.net/admin/?logged_out=true301
https://smallshop.example.com/?all_items=1https://globalstore.example.net/?all_items=1301
http://example.com/about/(unchanged)n/a

​​ Redirect admin area requests to HTTPS

This example dynamic redirect for zone example.com will redirect requests for the administration area of a specific subdomain (store.example.com) to HTTPS, keeping the original path and query string.

When incoming requests match

  • Field: SSL/HTTPS
  • Value: Off

And

  • Field: Hostname
  • Operator: equals
  • Value: store.example.com

And

  • Field: URI Path
  • Operator: starts with
  • Value: /admin

If you are using the Expression Editor, enter the following expression:
(not ssl and http.host eq "store.example.com" and starts_with(http.request.uri.path, "/admin"))

Then

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

The rule includes SSL/HTTPS: Off (not ssl in the rule expression) to avoid redirect loops.

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

Request URLTarget URLStatus code
http://store.example.com/admin/products/https://store.example.com/admin/products/301
https://store.example.com/admin/products/(unchanged)n/a
http://store.example.com/admin/?logged_out=truehttps://store.example.com/admin/?logged_out=true301
http://store.example.com/?all_items=true(unchanged)n/a
http://example.com/admin/(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

​​ Perform mobile redirects

The following examples will redirect visitors using mobile devices — based on the request user agent string — to a different hostname.

​​ Redirect mobile users dropping the original URI path

This example static redirect will redirect requests for the current zone (example.com) from mobile users to m.example.com without preserving the URI path in the original HTTP request.

When incoming requests match

  • Enter the following expression in the Expression Editor:
    not http.host in {"m.example.com"} and (http.user_agent contains "mobi" or http.user_agent contains "Mobi")

Then

  • Type: Static
  • URL: m.example.com
  • Status code: 301

Notes about this example:

  • The not http.host in {"m.example.com"} condition prevents redirect loops.
  • The user agent condition follows Mozilla’s recommendation for identifying mobile devices.
  • The Then > URL value should be the same as the one you entered in the http.host condition of the rule’s filter expression.
  • You can redirect users to other zones on Cloudflare or to other hostnames not on Cloudflare.

​​ Redirect mobile users keeping the original path

This example dynamic redirect will redirect requests for the current zone (example.com) from mobile users to m.example.com, keeping the URI path of the original HTTP request.

When incoming requests match

  • Enter the following expression in the Expression Editor:
    not http.host in {"m.example.com"} and (http.user_agent contains "mobi" or http.user_agent contains "Mobi")

Then

  • Type: Dynamic
  • Expression: concat("https://m.example.com", http.request.uri.path)
  • Status code: 301

Notes about this example:

  • The not http.host in {"m.example.com"} condition prevents redirect loops.
  • The user agent condition follows Mozilla’s recommendation for identifying mobile devices.
  • The hostname in Then > Expression should be the same as the one you entered in the http.host condition of the rule’s filter expression.
  • Depending on your use case, you may want to enable Then > Preserve query string to also keep the query string of the original request.
  • You can redirect users to other zones on Cloudflare or to other hostnames not on Cloudflare.