Configuring request redirection
With request redirection, you can reconfigure paths of requests to your content. This can be useful when restructuring files and folders on an origin. You can also use redirection if you want to keep search engine rankings of your content when moving your resource to a new address.
To enable request redirection on a CDN resource:
-
In the management console
, select the folder where your resource is located. -
Select Cloud CDN.
-
Click the CDN resource name.
-
In the top-right corner, click
Edit. -
Enable Redirecting requests.
-
In the Rewrite rule field, set a rule, e.g.,
/(.*) /new-folder/$1
.A rewrite rule must contain two space-separated directives: the original path you need to replace, and the edited path, which replaces the original path.
You can use regular expressions in the rule. For more information, see Rewrite rule.
-
In the Flag field, specify the required flag:
break
: Terminates the processing of the current set of directives.last
: Terminates the processing of the current set of directives and starts searching for a new CDN server that matches the new URI.redirect
: Returns a temporaryredirect
with the302
status code to the user. This flag is used if the replacement string does not start withhttp://
,https://
, or$scheme
.permanent
: Returns a permanentredirect
with the301
status code to the user.
-
Click Save.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
View the description of the CLI update resource command:
yc cdn resource update --help
-
Get a list of all resources in the default folder:
yc cdn resource list --format yaml
Result:
- id: bc8ceo77zu7h******** folder_id: b1gt6g8ht345******** cname: cdn.example.com created_at: "2024-09-10T19:15:16.305043Z" updated_at: "2024-09-11T17:19:50.622628Z" active: true options: edge_cache_settings: enabled: true default_value: "345600" query_params_options: ignore_query_string: enabled: true value: true slice: enabled: true value: true host_options: host: enabled: true value: cdn-source-sample-bucket.storage.yandexcloud.net stale: enabled: true value: - error - updating ignore_cookie: enabled: true value: true origin_group_id: "328146" origin_group_name: Origins for cdn.example.com (225300) origin_protocol: HTTPS ssl_certificate: type: CM status: READY data: cm: id: fpquvh0em4no********
Save the CDN resource ID (the
id
field at the beginning of the output) as you will need it at the next step. -
Enable request redirection by specifying the CDN resource ID you saved in the previous step:
yc cdn resource update <resource_ID> \ --rewrite-body '<rewrite_rule>' \ --rewrite-flag <flag>
Where:
-
--rewrite-body
: Rewrite rule, e.g.,--rewrite-body '/(.*) /new-folder/$1'
.A rewrite rule must contain two space-separated directives: the original path you need to replace, and the edited path, which replaces the original path.
You can use regular expressions in the rule. For more information, see Rewrite rule.
-
--rewrite-flag
: Flag. The possible values are:break
: Terminates the processing of the current set of directives.last
: Terminates the processing of the current set of directives and starts searching for a new CDN server that matches the new URI.redirect
: Returns a temporaryredirect
with the302
status code to the user. This flag is used if the replacement string does not start withhttp://
,https://
, or$scheme
.permanent
: Returns a permanentredirect
with the301
status code to the user.
Result
id: bc8ceo77zu7h******** folder_id: b1gt6g8ht345******** cname: cdn.example.com created_at: "2024-09-10T19:15:16.305043Z" updated_at: "2024-09-11T17:55:21.100567Z" active: true options: edge_cache_settings: enabled: true default_value: "345600" query_params_options: ignore_query_string: enabled: true value: true slice: enabled: true value: true host_options: host: enabled: true value: cdn-source-sample-bucket.storage.yandexcloud.net stale: enabled: true value: - error - updating ignore_cookie: enabled: true value: true rewrite: enabled: true body: /(.*) /new-folder/$1 flag: BREAK origin_group_id: "328146" origin_group_name: Origins for cdn.example.com (225300) origin_protocol: HTTPS ssl_certificate: type: CM status: READY data: cm: id: fpquvh0em4no********
To disable request redirection for a CDN resource, run this command, specifying the appropriate CDN resource ID:
yc cdn resource update <resource_ID> --clear-rewrite
For more information about the
yc cdn resource update
command, see the CLI reference. -
Use the update REST API method for the Resource resource or the ResourceService/Update gRPC API call.
It may take up to 15 minutes for the new settings of the existing resource to apply to CDN servers. After that, we recommend purging the resource cache.