Cloudflare 301 Redirect Rule Configuration
This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文
* Single Redirects — Example rules · Cloudflare Rules docs - Cloudflare Docs
* [https://developers.cloudflare.com/rules/url-forwarding/single-redirects/examples/](https://developers.cloudflare.com/rules/url-forwarding/single-redirects/examples/)
* The following sections contain example single redirect rule configurations.
* 2024-06-06 21:36:14
Example rules
The following sections contain example single redirect rule configurations.
Redirect visitors to a page's new URL
This example static redirect for the example.com zone redirects 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 use 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 will perform the following redirects:
| Request URL | Target URL | Status code |
|---|---|---|
example.com/contact-us/ |
example.com/contacts/ |
301 |
example.com/contact-us/?state=TX |
example.com/contacts/?state=TX |
301 |
example.com/team/ |
(unchanged) | None |
Redirect all requests to a different hostname (the main one I need)
This example dynamic redirect redirects all requests to smallshop.example.com to a different hostname using HTTPS, preserving the original path and query string.
When incoming requests match
- Field: Hostname
- Operator: equals
- **Value:
smallshop.example.com
If you use 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 will perform the following redirects:
| Request URL | Target URL | Status code |
|---|---|---|
http://smallshop.example.com/ |
https://globalstore.example.net/ |
301 |
http://smallshop.example.com/admin/?logged_out=true |
https://globalstore.example.net/admin/?logged_out=true |
301 |
https://smallshop.example.com/?all_items=1 |
https://globalstore.example.net/?all_items=1 |
301 |
http://example.com/about/ |
(unchanged) | None |
Redirect admin area requests to HTTPS
This example dynamic redirect for the example.com zone redirects requests to the admin area of a specific subdomain ( store.example.com) to HTTPS, preserving 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 use 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 will perform the following redirects:
| Request URL | Target URL | Status code |
|---|---|---|
http://store.example.com/admin/products/ |
https://store.example.com/admin/products/ |
301 |
https://store.example.com/admin/products/ |
(unchanged) | None |
http://store.example.com/admin/?logged_out=true |
https://store.example.com/admin/?logged_out=true |
301 |
http://store.example.com/?all_items=true |
(unchanged) | None |
http://example.com/admin/ |
(unchanged) | None |
Redirect visitors from the UK and France to their respective subdomains
This example dynamic redirect for the example.com zone redirects visitors from the UK and France requesting the site's root path ( /) to their localized subdomains, https://gb.example.com and https://fr.example.com, respectively.
When incoming requests match
Use 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 will perform the following redirects:
| Visitor country | Request URL | Target URL | Status code |
|---|---|---|---|
| United Kingdom | example.com |
https://gb.example.com |
301 |
| France | example.com |
https://fr.example.com |
301 |
| United States | example.com |
(unchanged) | None |
Remove locale information from the URL path
This example dynamic redirect for the example.com zone redirects visitors from an old URL format containing a locale (such as /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 regex\_replace() function lets you extract parts of a URL using regex capture groups. Create a capture group by enclosing part of the regular expression in parentheses. Then use ${<num>} in the replacement string to reference a capture group, where <num> is the capture group number.
For example, the redirect rule performs the following redirects:
| Request URL | Target URL | Status code |
|---|---|---|
example.com/en-us/meet-our-team |
example.com/meet-our-team |
301 |
example.com/pt-BR/meet-our-team |
example.com/meet-our-team |
301 |
example.com/en-us/calendar?view=month |
example.com/calendar?view=month |
301 |
example.com/meet-our-team |
(unchanged) | None |
example.com/robots.txt |
(unchanged) | None |
Redirect mobile users
The following examples redirect visitors using mobile devices to a different hostname based on the request's user agent string.
Redirect mobile users and remove the original URI path
This example static redirect redirects mobile users' requests to the current zone (example.com) to m.example.com without preserving the URI path from 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 on this example:
- The
not http.host in {"m.example.com"}condition prevents redirect loops. - The user agent conditions follow Mozilla's recommendations for identifying mobile devices.
- The Then > URL value should match the value you entered in the
http.hostcondition of the rule's filter expression. - You can redirect users to other zones on Cloudflare or to hostnames outside Cloudflare.
Redirect mobile users and preserve the original path
This example dynamic redirect redirects mobile users' requests to the current zone (example.com) to m.example.com, preserving the URI path from 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 on this example:
- The
not http.host in {"m.example.com"}condition prevents redirect loops. - The user agent conditions follow Mozilla's recommendations for identifying mobile devices.
- The hostname in Then > Expression should match the hostname you entered in the
http.hostcondition of the rule's filter expression. - Depending on your use case, you may need to enable Then > Preserve query string to preserve the original request's query string.
- You can redirect users to other zones on Cloudflare or to hostnames outside Cloudflare.
Comments 0