Skip to main content

Create a rule manually or from a template to configure settings for particular files

The Rules section overrides CDN options for specific files — independently of the Resource Settings that apply to all files by default. Each rule matches files by URI pattern or regular expression and applies its own option set to those files only, with a maximum of five rules per CDN resource.

As an example: CDN caching settings are enabled for a resource with the CDN-controlled option, which applies to all files. To apply Origin-Controlled to specific files instead, create a rule for those paths and add the Origin-Controlled option — CDN-Controlled then applies everywhere except the paths covered by the rule.

The Rules tab shows how many rules have been created for the resource.

Rules section with rule count and Create rule button

Add rules in the customer portal

Rules are added from the Rules tab of any CDN resource. Each rule can be built from scratch or created from a template.

Create a rule manually

1. In the Resource settings, navigate to the Rules tab. Click Create rule and select Create blank rule from the drop-down list.

Create a rule manually

A new page will open. Follow the remaining steps on that page.

Create a rule manually

2. Enter the rule name.

3. Specify the path to the file or folder to which the rule applies. The path must start with ^/ or / and cannot contain the origin domain name. Specify a URI or use regular expressions (regex).

If a URI matches multiple rules, the rule that appears first in the order is applied. For instance, if the resource has two rules — /folder/.* (first) and /folder/image.jpg (second) — the first rule applies to /folder/image.jpg.

4. If the CDN resource has multiple origin groups, select the required group from the Origins Group list.

5. Select the protocol that the CDN resource will use to pull the files specified in step three. If files are accessible over the same protocol as the origin, select Inherit from resource. If not, select the appropriate option: HTTP, HTTPS, or both protocols at once.

6. Click the Add option button to configure options if necessary. A panel opens with the list of available options — click an option name to add it to the rule. When done, close the panel.

Add option panel with available CDN options

There are three variants for how the options in the Rules sections interact with the options in the Resource Settings:

VariantInteraction methodExample
Options are not added to the ruleOption parameters are inherited from the Resource settingsCDN caching is enabled in the Resource settings and the rule contains no CDN caching option — caching parameters for the files in the rule are inherited from the Resource settings.
An option is added to the rule, but is disabledOption parameters from the Resource Settings are disabledThe Country access policy option is enabled with Allow by default and DE in the Resource settings, blocking access to content in Germany. A rule with the disabled Country access policy option means that files in the rule remain accessible in Germany.
An option is added to the rule and enabledOption parameters from the Resource Settings are overriddenBrowser caching is set to CDN-Controlled, 4 days in the Resource settings. A rule with Browser caching set to 30 minutes overrides that for the files in the rule — they are cached in the browser for 30 minutes while other files are cached for 4 days.

7. Click Create rule.

Create a rule from a template

Templates allow a fixed set of options to be reused across multiple CDN resources. Five system templates are available — Video segments (.ts), Playlists (.m3u8), Let's Encrypt HTTP-01 challenge, Static content, and Image optimization — and custom templates can be created from any saved rule. Full details are in the templates guide.

To create a rule from a template:

1. In the Resource settings, navigate to the Rules tab. Click Create rule. In the dropdown, select Create blank rule to build a rule from scratch, or select a template name under Create from system templates to use a system template, or select a custom template created earlier.

Create rule dropdown showing system templates and custom templates

2. A form opens with pre-filled settings from the template. Review the settings and edit them if needed — the steps in Create a rule apply.

3. Click Create rule.

Create rule from template form with pre-filled settings

Examples of rules

The following examples show how rules can be used to override specific CDN options for a subset of files.

Prohibit caching for static files

To prohibit CDN caching for JPG, CSS, and JS files:

  • Enter ^/.*\.(jpg|css|js)$ in the Rule pattern field.
  • Add the CDN caching option with the "CDN Controlled" and "Do not cache" values.
CDN caching disabled for static files matching the pattern

Set Access-Control-Allow-Origin for .ttf files

To set up the Access-Control-Allow-Origin HTTP header only for files with the .ttf extension:

  • Enter /.*\.ttf$ in the Rule pattern field.
  • Add the CORS header support option.
  • Configure which domains the header applies to — CORS header support explains the available options.
Set Access-Control-Allow-Origin for .ttf files

Suspend, activate, or delete a rule

Rules can be suspended when not needed — for testing purposes or before deletion. Suspended rules do not apply to the specified files but still count toward the rule limit.

To suspend a rule:

1. Navigate to the Rules tab and click the rule name to open its settings.

Rules section with rule name link

2. In the Rule availability section, uncheck Enable rule. A confirmation dialog appears — click Yes, suspend.

Rule availability section with Enable rule checkbox unchecked

To activate a suspended rule, click the rule name, check Enable rule, and click Save changes.

Suspended rules have a Suspended status and active rules have an Active status in the rules list.

Suspended status and Active status in the rules list

Alternatively, use the three-dot menu next to any rule in the list. For active rules, the menu provides Suspend rule — clicking it opens the same confirmation dialog. For suspended rules, the menu provides Activate rule — clicking it activates the rule immediately, without a confirmation dialog.

The three-dot menu also provides Edit to open the rule settings, Save as template to create a template from the current rule configuration, and Delete rule to delete the rule. Only suspended rules can be deleted.

To delete a rule:

1. Suspend the rule (see above).

2. Click the three-dot menu next to the suspended rule and select Delete rule.

3. Click Yes, delete to confirm.

Create a rule that overrides CDN resource settings for matching file paths. A CDN resource can have up to five rules. The CDN resource ID is visible in the Customer Portal under CDN → CDN Resources, or from the List CDN resources API call.

An API token is required.

export GCORE_API_KEY="{YOUR_API_KEY}"
export CDN_RESOURCE_ID="{YOUR_RESOURCE_ID}"

Create a rule

Match files with a regular expression. ruleType 0 requires a pattern that starts with ^/ or /. If a URI matches more than one rule, the rule with the lower weight is applied first. Options omitted from the rule stay inherited from the CDN resource. A Customer Portal template pre-fills these fields — send the same values in the create request.

ParameterRequiredDescription
nameYesRule label
ruleYesURI pattern or regular expression, without a domain
ruleTypeYes0 for a regular expression that starts with ^/ or /
activeNotrue applies the rule. Defaults to true
weightNoExecution order, lowest first. Defaults to 1
originGroupNoOrigin group ID. When omitted, the resource origin group is used
optionsNoOption overrides for matching files
from gcore import CDB

client = CDB()

rule = client.cdn.cdn_resources.rules.create(
    12345,  # CDN resource ID
    name="no-cache-static",
    rule=r"^/.*\.(jpg|css|js)$",
    rule_type=0,
    active=True,
    options={
        "edge_cache_settings": {"enabled": True, "value": "0s"},
    },
)
print(rule.id, rule.name, rule.rule, rule.active)
package main

import (
    "context"
    "fmt"

    gcore "github.com/G-Core/gcore-go"
    "github.com/G-Core/gcore-go/cdn"
)

func main() {
    client := gcore.NewClient()
    ctx := context.Background()

    rule, err := client.CDN.CDNResources.Rules.New(ctx, 12345, cdn.CDNResourceRuleNewParams{
        Name:     "no-cache-static",
        Rule:     `^/.*\.(jpg|css|js)$`,
        RuleType: 0,
        Active:   gcore.Bool(true),
    })
    if err != nil {
        panic(err)
    }
    fmt.Println(rule.ID, rule.Name, rule.Rule, rule.Active)
}
curl -X POST "https://api.cdb-staging.cdn.orange.com/cdn/resources/$CDN_RESOURCE_ID/rules" \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "no-cache-static",
    "rule": "^/.*\\.(jpg|css|js)$",
    "ruleType": 0,
    "active": true
  }'

The API returns HTTP 201:

{
  "id": 10001,
  "name": "no-cache-static",
  "rule": "^/.*\\.(jpg|css|js)$",
  "ruleType": 0,
  "active": true,
  "weight": 1
}

List and get rules

List every rule on a CDN resource, or retrieve one rule by ID.

from gcore import CDB

client = CDB()

for rule in client.cdn.cdn_resources.rules.list(12345, limit=10):
    print(rule.id, rule.name, rule.active, rule.rule)

rule = client.cdn.cdn_resources.rules.get(10001, resource_id=12345)
print(rule.id, rule.rule, rule.weight)
package main

import (
    "context"
    "fmt"

    gcore "github.com/G-Core/gcore-go"
    "github.com/G-Core/gcore-go/cdn"
)

func main() {
    client := gcore.NewClient()
    ctx := context.Background()

    page, err := client.CDN.CDNResources.Rules.List(ctx, 12345, cdn.CDNResourceRuleListParams{})
    if err != nil {
        panic(err)
    }
    for _, rule := range page.Results {
        fmt.Println(rule.ID, rule.Name, rule.Active)
    }

    rule, err := client.CDN.CDNResources.Rules.Get(ctx, 10001, cdn.CDNResourceRuleGetParams{
        ResourceID: 12345,
    })
    if err != nil {
        panic(err)
    }
    fmt.Println(rule.ID, rule.Rule)
}
curl -X GET "https://api.cdb-staging.cdn.orange.com/cdn/resources/$CDN_RESOURCE_ID/rules?limit=10" \
  -H "Authorization: APIKey $GCORE_API_KEY"

curl -X GET "https://api.cdb-staging.cdn.orange.com/cdn/resources/$CDN_RESOURCE_ID/rules/10001" \
  -H "Authorization: APIKey $GCORE_API_KEY"

A list request returns a paginated object:

{
  "count": 1,
  "next": null,
  "previous": null,
  "results": [
    {
      "id": 10001,
      "name": "no-cache-static",
      "active": true,
      "rule": "^/.*\\.(jpg|css|js)$"
    }
  ]
}

Suspend or activate a rule

Set active to false to suspend a rule. A suspended rule does not apply to matching files, but still counts toward the five-rule limit. Set active to true to apply the rule again.

from gcore import CDB

client = CDB()

rule = client.cdn.cdn_resources.rules.update(
    10001,
    resource_id=12345,
    active=False,
)
print(rule.id, rule.active)
package main

import (
    "context"
    "fmt"

    gcore "github.com/G-Core/gcore-go"
    "github.com/G-Core/gcore-go/cdn"
)

func main() {
    client := gcore.NewClient()
    ctx := context.Background()

    rule, err := client.CDN.CDNResources.Rules.Update(ctx, 10001, cdn.CDNResourceRuleUpdateParams{
        ResourceID: 12345,
        Active:     gcore.Bool(false),
    })
    if err != nil {
        panic(err)
    }
    fmt.Println(rule.ID, rule.Active)
}
curl -X PATCH "https://api.cdb-staging.cdn.orange.com/cdn/resources/$CDN_RESOURCE_ID/rules/10001" \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"active": false}'

The API returns HTTP 200. active is false when the rule is suspended:

{
  "id": 10001,
  "name": "no-cache-static",
  "active": false
}

Delete a rule

Deactivate the rule first. A delete request against an active rule returns HTTP 400. After the rule is suspended, delete it. A successful delete returns HTTP 204.

from gcore import CDB

client = CDB()

client.cdn.cdn_resources.rules.update(
    10001,
    resource_id=12345,
    active=False,
)
client.cdn.cdn_resources.rules.delete(10001, resource_id=12345)
package main

import (
    "context"

    gcore "github.com/G-Core/gcore-go"
    "github.com/G-Core/gcore-go/cdn"
)

func main() {
    client := gcore.NewClient()
    ctx := context.Background()

    _, err := client.CDN.CDNResources.Rules.Update(ctx, 10001, cdn.CDNResourceRuleUpdateParams{
        ResourceID: 12345,
        Active:     gcore.Bool(false),
    })
    if err != nil {
        panic(err)
    }
    if err := client.CDN.CDNResources.Rules.Delete(ctx, 10001, cdn.CDNResourceRuleDeleteParams{
        ResourceID: 12345,
    }); err != nil {
        panic(err)
    }
}
curl -X PATCH "https://api.cdb-staging.cdn.orange.com/cdn/resources/$CDN_RESOURCE_ID/rules/10001" \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"active": false}'

curl -X DELETE "https://api.cdb-staging.cdn.orange.com/cdn/resources/$CDN_RESOURCE_ID/rules/10001" \
  -H "Authorization: APIKey $GCORE_API_KEY"

A successful delete returns HTTP 204 with an empty body.

Declare CDN resource rules that override delivery options for matching paths with the Terraform provider v2. A CDN resource can have up to five rules. Options omitted from the rule stay inherited from the CDN resource. Suspended rules still count toward that limit.

Create a rule

Match files with a regular expression. rule_type 0 requires a pattern that starts with ^/ or /. If a URI matches more than one rule, the rule with the lower weight is applied first. A Customer Portal template pre-fills these fields — declare the same values in the rule block.

resource "gcore_cdn_origin_group" "example" {
  name = "my-origin-group"

  sources = [
    {
      source  = "example.com"
      enabled = true
    }
  ]
}

resource "gcore_cdn_resource" "example" {
  cname           = "cdn.example.com"
  origin_group    = gcore_cdn_origin_group.example.id
  origin_protocol = "HTTPS"
}

resource "gcore_cdn_resource_rule" "no_cache_static" {
  resource_id = gcore_cdn_resource.example.id
  name        = "no-cache-static"
  rule        = "^/.*\\.(jpg|css|js)$"
  rule_type   = 0
  active      = true
  weight      = 1

  options = {
    edge_cache_settings = {
      enabled = true
      value   = "0s"
    }
  }
}

After terraform apply, matching JPG, CSS, and JS paths use this rule instead of the CDN resource defaults. edge_cache_settings is 0s, so those files are not cached on CDN servers.

Suspend or activate a rule

Set active to false and run terraform apply to suspend the rule. The rule stops applying to matching files, while it still counts toward the five-rule limit. Set active to true and apply again to restore it.

resource "gcore_cdn_origin_group" "example" {
  name = "my-origin-group"

  sources = [
    {
      source  = "example.com"
      enabled = true
    }
  ]
}

resource "gcore_cdn_resource" "example" {
  cname           = "cdn.example.com"
  origin_group    = gcore_cdn_origin_group.example.id
  origin_protocol = "HTTPS"
}

resource "gcore_cdn_resource_rule" "no_cache_static" {
  resource_id = gcore_cdn_resource.example.id
  name        = "no-cache-static"
  rule        = "^/.*\\.(jpg|css|js)$"
  rule_type   = 0
  active      = false
  weight      = 1

  options = {
    edge_cache_settings = {
      enabled = true
      value   = "0s"
    }
  }
}

Delete a rule

Remove the rule block — Terraform deletes the rule on the next terraform apply. The origin group and CDN resource stay managed.

resource "gcore_cdn_origin_group" "example" {
  name = "my-origin-group"

  sources = [
    {
      source  = "example.com"
      enabled = true
    }
  ]
}

resource "gcore_cdn_resource" "example" {
  cname           = "cdn.example.com"
  origin_group    = gcore_cdn_origin_group.example.id
  origin_protocol = "HTTPS"
}

# Remove or comment out this block:
# resource "gcore_cdn_resource_rule" "no_cache_static" {
#   resource_id = gcore_cdn_resource.example.id
#   name        = "no-cache-static"
#   rule        = "^/.*\\.(jpg|css|js)$"
#   rule_type   = 0
#   active      = true
#   weight      = 1
#
#   options = {
#     edge_cache_settings = {
#       enabled = true
#       value   = "0s"
#     }
#   }
# }
terraform apply