The following sections describe the settings of redirect rules to configure static and dynamic URL redirects.
Performs a URL redirect using wildcard patterns to match multiple requests. This method simplifies defining source and target URL patterns without needing complex expressions.
A wildcard URL redirect has the following configuration parameters:
-
Request URL: Enter the wildcard pattern using the asterisk (
*) character to match multiple requests. For example,https://*.example.com/files/*. Each*matches any characters, including none. Matching ignores upper and lower case, but the captured values keep their original case. -
Target URL: Enter the target URL, which can be static (for example,
https://example.com) or dynamic (for example,https://example.com/${1}/files/${2}). Use wildcard references such as${1}and${2}to define dynamic targets. Refer to How wildcard references are numbered. -
Status code: The HTTP status code of the redirect response (301 - Permanent Redirect by default). Browsers can cache
301and308redirects, and Cloudflare does not add aCache-Controlheader to redirect responses, so consider302or307while you test a rule. Must be one of the following:- 301 - Permanent Redirect: The page has permanently moved to a new address. For
POSTrequests, the client or browser might switch the HTTP method toGETwhen following the redirect. - 302 - Temporary Redirect: The page has temporarily moved to a new address. For
POSTrequests, the client or browser might switch the HTTP method toGETwhen following the redirect. - 307 - Advanced: Temporary, HTTP method preserved: The page has temporarily moved to a new address. The client or browser must preserve the original HTTP method (for example,
POST) when following the redirect. - 308 - Advanced: Permanent, HTTP method preserved: The page has permanently moved to a new address. The client or browser must preserve the original HTTP method (for example,
POST) when following the redirect.
- 301 - Permanent Redirect: The page has permanently moved to a new address. For
-
Preserve query string: Whether to preserve the query string when redirecting (disabled by default). When enabled, Cloudflare replaces any query string in the target URL with the query string of the original request. If the original request has no query string, the redirect URL has no query string. With a wildcard pattern, if the request URL ends with
*and the target URL ends with the matching wildcard reference, the redirect keeps the query string even when this setting is disabled. Refer to Query strings in wildcard patterns.
API information
Wildcard URL redirects are regular dynamic URL redirects that use the wildcard_replace() function in the target_url.expression parameter.
The full syntax of the "action_parameters" field for a redirect rule performing a wildcard URL redirect is the following:
"action_parameters": {
"from_value": {
"target_url": {
"expression": "wildcard_replace(http.request.full_uri, r\"<REQUEST_URL_PATTERN>\", r\"<TARGET_URL_PATTERN>\")"
},
"status_code": <STATUS_CODE>,
"preserve_query_string": <BOOLEAN_VALUE>
}
}The required parameters are <REQUEST_URL_PATTERN> and <TARGET_URL_PATTERN>.
The optional parameters can have the following values:
"status_code"(integer):301(permanent redirect)302(temporary redirect)307(temporary redirect, preserving original HTTP method)308(permanent redirect, preserving original HTTP method)
"preserve_query_string"(boolean):trueorfalse(falseif omitted)
Cloudflare numbers each * in the Request URL from left to right, starting at ${1}. This includes the * in http*.
For example, with the request URL http*://example.com/blog/*:
| Reference | Captures | Value for https://example.com/blog/hello |
Value for http://example.com/blog/hello |
|---|---|---|---|
${1} |
The * in http* |
s |
(empty) |
${2} |
The * after /blog/ |
hello |
hello |
To keep the path in the target URL, use ${2} (for example, https://blog.example.com/${2}).
To match only HTTPS requests, write the scheme explicitly. With the request URL https://example.com/blog/*, the path is ${1}.
For a complete example, refer to Redirect a website section to a subdomain.
Cloudflare matches the request URL against the full URL, including the query string. A request URL that does not end with *, such as https://example.com/contact, does not match https://example.com/contact?ref=1. To match a page with or without a query string, use a custom filter expression on the URI path, or end the request URL with *.
A * at the end of the request URL captures the rest of the path and the query string. For example, with the request URL http*://example.com/blog/* and the request https://example.com/blog/hello?page=2, ${2} is hello?page=2.
As a result, a target URL that ends with a wildcard reference, such as https://blog.example.com/${2}, keeps the query string even if Preserve query string is disabled.
If Preserve query string is enabled, the query string appears only once in the redirect URL.
To redirect without the query string, use a custom filter expression instead of a wildcard pattern. Select the Dynamic type and build the target URL from the URI path only, for example wildcard_replace(http.request.uri.path, "/blog/*", "https://blog.example.com/${1}"), and keep Preserve query string disabled. With this rule, https://example.com/blog/post?page=2 redirects to https://blog.example.com/post.
Performs a static URL redirect with a given HTTP status code and optionally preserves the query string.
A static URL redirect has the following configuration parameters:
-
URL: A literal string that will be used in the
LocationHTTP header returned in the redirect response. Include the URI scheme, such ashttps://example.com/. Cloudflare sends a URL without a scheme, such asexample.com, unchanged, and browsers treat it as a relative path on the current hostname. -
Status code: The HTTP status code of the redirect response (301 - Permanent Redirect by default). Browsers can cache
301and308redirects, and Cloudflare does not add aCache-Controlheader to redirect responses, so consider302or307while you test a rule. Must be one of the following:- 301 - Permanent Redirect: The page has permanently moved to a new address. For
POSTrequests, the client or browser might switch the HTTP method toGETwhen following the redirect. - 302 - Temporary Redirect: The page has temporarily moved to a new address. For
POSTrequests, the client or browser might switch the HTTP method toGETwhen following the redirect. - 307 - Advanced: Temporary, HTTP method preserved: The page has temporarily moved to a new address. The client or browser must preserve the original HTTP method (for example,
POST) when following the redirect. - 308 - Advanced: Permanent, HTTP method preserved: The page has permanently moved to a new address. The client or browser must preserve the original HTTP method (for example,
POST) when following the redirect.
- 301 - Permanent Redirect: The page has permanently moved to a new address. For
-
Preserve query string: Whether to preserve the query string when redirecting (disabled by default). When enabled, Cloudflare replaces any query string in the target URL with the query string of the original request. If the original request has no query string, the redirect URL has no query string.
API information
The full syntax of the "action_parameters" field for a redirect rule performing a static URL redirect is the following:
"action_parameters": {
"from_value": {
"target_url": {
"value": "<STATIC_URL_VALUE>"
},
"status_code": <STATUS_CODE>,
"preserve_query_string": <BOOLEAN_VALUE>
}
}The only required parameter is <STATIC_URL_VALUE>.
The optional parameters can have the following values:
"status_code"(integer):301(permanent redirect)302(temporary redirect)307(temporary redirect, preserving original HTTP method)308(permanent redirect, preserving original HTTP method)
"preserve_query_string"(boolean):trueorfalse(falseif omitted)
Performs a dynamic URL redirect, where the target URL is determined by an expression. You can configure the redirect HTTP status code and whether to preserve the query string when redirecting.
A dynamic URL redirect has the following configuration parameters:
-
Expression: An expression that defines the target URL of the redirect. The result of evaluating this expression will be used in the
LocationHTTP header returned in the redirect response. Refer to the fields and functions you can use in expressions. -
Status code: The HTTP status code of the redirect response (301 - Permanent Redirect by default). Browsers can cache
301and308redirects, and Cloudflare does not add aCache-Controlheader to redirect responses, so consider302or307while you test a rule. Must be one of the following:- 301 - Permanent Redirect: The page has permanently moved to a new address. For
POSTrequests, the client or browser might switch the HTTP method toGETwhen following the redirect. - 302 - Temporary Redirect: The page has temporarily moved to a new address. For
POSTrequests, the client or browser might switch the HTTP method toGETwhen following the redirect. - 307 - Advanced: Temporary, HTTP method preserved: The page has temporarily moved to a new address. The client or browser must preserve the original HTTP method (for example,
POST) when following the redirect. - 308 - Advanced: Permanent, HTTP method preserved: The page has permanently moved to a new address. The client or browser must preserve the original HTTP method (for example,
POST) when following the redirect.
- 301 - Permanent Redirect: The page has permanently moved to a new address. For
-
Preserve query string: Whether to preserve the query string when redirecting (disabled by default). When enabled, Cloudflare replaces any query string in the target URL with the query string of the original request. If the original request has no query string, the redirect URL has no query string.
API information
The full syntax of the "action_parameters" field for a redirect rule performing a dynamic URL redirect is the following:
"action_parameters": {
"from_value": {
"target_url": {
"expression": "<DYNAMIC_URL_EXPRESSION>"
},
"status_code": <STATUS_CODE>,
"preserve_query_string": <BOOLEAN_VALUE>
}
}The only required parameter is <DYNAMIC_URL_EXPRESSION>.
The optional parameters can have the following values:
"status_code"(integer):301(permanent redirect)302(temporary redirect)307(temporary redirect, preserving original HTTP method)308(permanent redirect, preserving original HTTP method)
"preserve_query_string"(boolean):trueorfalse(falseif omitted)