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

# Setting up Yandex Cloud CDN location rules

_Location rules_ are a mechanism used to selectively redefine [CDN resource](../../concepts/resource.md) settings for requests whose paths match a specific template.

> Let’s assume that [content caching](../../concepts/caching.md) is enabled in the CDN resource settings, while cache lifetime is set to four days. This is enough for most types of content, but for some specific files you have to reduce cache lifetime to a few seconds. Location rules will help you with such cases.

[Learn more about location rules](../../concepts/location-rules.md)

To set up a location rule:
1. [Create](#create) a rule.
1. [Configure](#setup) advanced settings.

## Creating a location rule {#create}

{% 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. Select the ![image](../../../_assets/console-icons/map-pin-plus.svg) **Location rules** tab.
  1. In the top-right corner, click **Add rule**.
  1. Under **Overview**, configure the main location rule settings:
      * **Name**.

        The rule name may contain lowercase Latin letters, numbers, underscores, hyphens, and spaces, and may be up to 50 characters long.
      * **Rule weight**: Rule triggering priority. The lower a rule’s weight, the higher its priority in the user resource configuration. If multiple rules have the same weight, they will be applied in a random order.

        <br>

        > For example, let’s assume you set the following location rules:
        >
        > | Rule | Weight | Regular expression | Cache lifetime |
        > | --- | --- | --- | --- |
        > | A | `1` | `^/path/.+` | `1 hour` |
        > | B | `2` | `\.txt$` | `4 hours` |
        >
        > With this configuration, the `/path/file.txt` request will match rule A, which has a lower weight and a higher priority than rule B, so the file will be cached for one hour.
        >
        > To cache the `/path/file.txt` file for four hours, rule B must have a lower weight than rule A.

  1. Under **Condition**, set **Regular expression**, i.e., the URI path pattern against which requests are checked. If there is a match, the settings specific to that location rule will apply.

      {% note tip %}
      
      To explicitly indicate the start or end of the string to match in a regular expression, use the `^` and `$` anchor characters.
      
      {% endnote %}

  1. Specify the CDN resource settings you want to redefine using a location rule:
      * **CDN caching**: Temporary storage of file copies from origins. For more information, see [Configuring resource caching](configure-caching.md).
      * **Redirect requests**: Redirecting content requests to a different path. This is useful when rearranging 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. For more information, see [Setting up a request redirect](setup-http-rewrite.md).
      * **Access via secure token**: Restricting direct access to content via pre-signed URLs with the limited validity period. For more information, see [Setting up access via a secure token](enable-secure-token.md).
      * **IP-based access**: Restricting access to content using an allowing or blocking policy. For more information, see [Updating the basic settings of a resource](configure-basics.md).

      {% note info %}
      
      The settings of HTTP headers and methods, CORS, content compression and segmentation, as well as advanced content caching options, become available after you create a location rule. For more details, see [Setting additional parameters](location-rules.md#setup).
      
      {% endnote %}

  1. Click **Create**.

- API {#api}

  Use the [create](../../api-ref/ResourceRules/create.md) REST API method for the [ResourceRules](../../api-ref/ResourceRules/index.md) resource or the [ResourceRulesService/Create](../../api-ref/grpc/ResourceRules/create.md) gRPC API call.

{% endlist %}

## Configuring advanced settings {#setup}

After creating a rule, you can configure its advanced settings:

{% 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. Select the ![image](../../../_assets/console-icons/map-pin-plus.svg) **Location rules** tab.
  1. Click the location rule name.
  1. Configure advanced rule settings:

      * To set up advanced content caching options, go to the ![image](../../../_assets/console-icons/cloud.svg) **Caching** tab and click ![image](../../../_assets/console-icons/pencil.svg) **Edit**. For more information, see [Configuring resource caching](configure-caching.md).
      * To configure HTTP request and response headers, CORS for responses to clients, and allowed HTTP methods for client requests, go to the ![image](../../../_assets/console-icons/arrow-up-to-line.svg) **HTTP headers and methods** tab and click ![image](../../../_assets/console-icons/pencil.svg) **Edit**. For more information, see:
          * [Configuring HTTP headers of requests and responses](configure-headers.md)
          * [Configuring CORS for responses to clients](configure-cors.md)
          * [Configuring HTTP methods](configure-http.md)
      * To configure file compression and segmentation, go to the ![image](../../../_assets/console-icons/box.svg) **Content** tab and click ![image](../../../_assets/console-icons/pencil.svg) **Edit**. For more information, see:
          * [Enabling file compression](enable-compression.md)
          * [Enabling file segmentation](enable-segmentation.md)

      Edit the relevant settings and click **Save**.

- API {#api}

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

{% endlist %}

### See also {#see-also}

* [Location rules Yandex Cloud CDN](../../concepts/location-rules.md)
* [Configuring resource caching](configure-caching.md)
* [Setting up a request redirect](setup-http-rewrite.md)
* [Setting up access via a secure token](enable-secure-token.md)
* [Updating the basic settings of a resource](configure-basics.md)