Wondering how to create bulk redirect via API in Cloudflare? We can help you.
At Bobcares, we offer solutions for every query, big and small, as a part of our Server Management Service.
Let’s take a look at how our Support Team create bulk redirect via API.
How to create bulk redirect via API in Cloudflare?
Usually, bulk Redirects allow us to define a large number of redirects at the account level.
These redirects navigate the user from a source URL to a target URL using a given HTTP status code.
Bulk Redirects are static which means that they do not support any string replacement operations or regular expressions.
However, we can configure URL Redirect parameters that affect their URL matching behavior and their runtime behavior.
Bulk Redirects involve the following elements:
1. URL Redirect: A simple object with a source URL, a target URL, a status code and redirect parameters.
URL Redirects are the list items of Bulk Redirect Lists.
2. Bulk Redirect List: A list, similar to an IP List, containing one or more URL Redirects.
To enable all the URL Redirects in a Bulk Redirect List, reference the list in a Bulk Redirect Rule.
Different Bulk Redirect Rules can reference the same Bulk Redirect List.
3. Bulk Redirect Rule: A rule powered by the Ruleset Engine, similar to a Transform Rue .
A Bulk Redirect Rule has an associated Bulk Redirect List.
Create Bulk Redirects via API
We can create bulk redirects in the dashboard as well as via API.
To create Bulk Redirects via API:
1. Firstly, create a Bulk Redirect List via API.
2. Then, add items (URL Redirects) to the Bulk Redirect list.
3. Finally, reate a Bulk Redirect Rule via API, which enables the Bulk Redirect list.
The API token used in API requests to manage Bulk Redirects objects (lists, list items, and rules) must have at least the following permissions:
- Account Rulesets: Edit
- Account Filter Lists: Edit
Create a Bulk Redirect List via API
Use the Create list operation to create a new Bulk Redirect List. The list kind must be redirect.
curl “https://api.cloudflare.com/client/v4/accounts//rules/lists” \
-H “Authorization: Bearer ” \
-H “Content-Type: application/json” \
-d ‘{
“name”: “our_redirect_list”,
“description”: “Our redirect list.”,
“kind”: “redirect”
}’
The response will be similar to the following:
{
“result”: {
“id”: “f848b6ccb07647749411f504d6f88794”,
“name”: “our_redirect_list”,
“description”: “Our redirect list.”,
“kind”: “redirect”,
“num_items”: 0,
“num_referencing_filters”: 0,
“created_on”: “2022-03-02T09:11:42Z”,
“modified_on”: “2022-03-02T09:11:42Z”
},
“success”: true,
“errors”: [],
“messages”: []
}
Add items to the list
Use the Create list items operation to add URL Redirect items to the list:
curl “https://api.cloudflare.com/client/v4/accounts//rules/lists/f848b6ccb07647749411f504d6f88794/items” \
-H “Authorization: Bearer ” \
-H “Content-Type: application/json” \
-d ‘[
{
“redirect”: {
“source_url”: “bobcares.com/blog/”,
“target_url”: “https://bobcares.com/blog/latest”
}
},
{
“redirect”: {
“source_url”: “bobcares.net/”,
“target_url”: “https://bobcares.net/under-construction.html”,
“status_code”: 307
}
}
]’
The response will be similar to the following:
{
“result”: {
“operation_id”: “92558f8b296d4dbe9d0419e0e53f6622”
},
“success”: true,
“errors”: [],
“messages”: []
}
This is an asynchronous operation.
The response will contain an operation_id which we will use to check if the operation completed successfully using the Get bulk operation method:
curl “https://api.cloudflare.com/client/v4/accounts//rules/lists/bulk_operations/92558f8b296d4dbe9d0419e0e53f6622” \
-H “Authorization: Bearer “
If the operation already completed successfully, the response will be similar to the following:
{
“result”: {
“id”: “92558f8b296d4dbe9d0419e0e53f6622”,
“status”: “completed”,
“completed”: “2022-03-02T09:15:42Z”
},
“success”: true,
“errors”: [],
“messages”: []
}
Create a Bulk Redirect Rule via API
Since Bulk Redirect Lists are just containers of URL Redirects, we have to enable the URL Redirects in the list by creating a Bulk Redirect Rule.
Add Bulk Redirect Rules to the entry point ruleset of the http_request_redirect phase at the account level.
A Bulk Redirect Rule must have:
- action set to redirect
- An action_parameters object with additional configuration settings
The following request of the Create account ruleset operation creates a phase entry point ruleset for the http_request_redirect phase at the account level, and defines a single redirect rule.
Use this operation if we have not created a phase entry point ruleset for the http_request_redirect phase yet.
curl “https://api.cloudflare.com/client/v4/accounts//rulesets” \
-H “Authorization: Bearer ” \
-H “Content-Type: application/json” \
-d ‘{
“name”: “Our redirect ruleset”,
“kind”: “root”,
“phase”: “http_request_redirect”,
“rules”: [
{
“expression”: “http.request.full_uri in $our_redirect_list”,
“description”: “Bulk Redirect rule.”,
“action”: “redirect”,
“action_parameters”: {
“from_list”: {
“name”: “our_redirect_list”,
“key”: “http.request.full_uri”
}
}
}
]
}’
The response will be similar to the following:
{
“result”: {
“id”: “528f4f03bf0da53a29907199625867be”,
“name”: “Our redirect ruleset”,
“kind”: “root”,
“version”: “1”,
“rules”: [
{
“id”: “8da312df846b4258a05bcd454ea943be”,
“version”: “1”,
“expression”: “http.request.full_uri in $our_redirect_list”,
“description”: “Bulk Redirect rule.”,
“action”: “redirect”,
“action_parameters”: {
“from_list”: {
“name”: “our_redirect_list”,
“key”: “http.request.full_uri”
}
},
“last_updated”: “2022-03-02T09:20:42Z”,
}
],
“last_updated”: “2022-03-02T09:20:42Z”,
“phase”: “http_request_redirect”
},
“success”: true,
“errors”: [],
“messages”: []
}
If there is already a phase entry point ruleset for the http_request_redirect phase, use the Update account ruleset operation instead.
Typically, error might look as shown below:
Error 10044: Target URL in redirect is too long
This error occurs when the target URL value of a URL redirect is too long. The maximum length of a target URL is 32,768 characters.
Today, let us see the simple solution provided by our Support Techs.
Specify a valid URL as the target URL.
[Looking for a solution to another query? We are just a click away.]
Conclusion
To sum up, our skilled Support Engineers at Bobcares demonstrated how to create bulk redirect via API in Cloudflare.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments