[Yandex Cloud documentation](../../../index.md) > [Yandex Cloud CDN](../../index.md) > [Step-by-step guides](../index.md) > Resources > Setting up a request redirect

# Setting up a request redirect

With [request redirection](../../concepts/http-rewrite.md), you can reconfigure paths of requests to your content. This can be useful when rearranging files and folders on an [origin](../../concepts/origins.md). 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:

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder where your resource is located.

  1. Navigate to **Cloud CDN**.

  1. Click the CDN resource name.

  1. In the top-right corner, click ![image](../../../_assets/console-icons/pencil.svg) **Edit**.

  1. Enable **Redirect requests**.

  1. 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](../../concepts/http-rewrite.md#rewrite-rule).

  1. In the **Flag** field, specify the required [flag](../../concepts/http-rewrite.md#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 temporary `redirect` with the `302` status code to the user. This flag is used if the replacement string does not start with `http://`, `https://`, or `$scheme`.
      * `permanent`: Returns a permanent `redirect` with the `301` status code to the user.

  1. Click **Save**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  1. View the description of the CLI command to update a resource:

      ```bash
      yc cdn resource update --help
      ```

  1. Get a list of all resources in the default folder:

      ```bash
      yc cdn resource list --format yaml
      ```

      Result:

      ```text
      - 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.

  1. Enable request redirection by specifying the CDN resource ID you saved in the previous step:

      ```bash
      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](../../concepts/http-rewrite.md#rewrite-rule).
      * `--rewrite-flag`: [Flag](../../concepts/http-rewrite.md#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 temporary `redirect` with the `302` status code to the user. This flag is used if the replacement string does not start with `http://`, `https://`, or `$scheme`.
          * `permanent`: Returns a permanent `redirect` with the `301` status code to the user.

      {% cut "Result" %}

      ```text
      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********
      ```

      {% endcut %}

     To disable request redirection for a CDN resource, run this command by specifying the CDN resource ID:

      ```bash
      yc cdn resource update <resource_ID> --clear-rewrite
      ```

      For more information about the `yc cdn resource update` command, see the [CLI reference](../../../cli/cli-ref/cdn/cli-ref/resource/update.md).

- API {#api}

  Use the [update](../../api-ref/Resource/update.md) REST API method for the [Resource](../../api-ref/Resource/index.md) resource or the [ResourceService/Update](../../api-ref/grpc/Resource/update.md) gRPC API call.

{% endlist %}

It may take up to 15 minutes for the new settings of the existing resource to apply to the CDN servers. After that, we recommend [purging the resource cache](purge-cache.md).

#### Useful links {#see-also}

* [Request redirection](../../concepts/http-rewrite.md)