> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gcore.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Set up a CDN resource to follow origin redirects

> Configure a Gcore CDN resource to follow origin redirects and cache the target response for selected status codes.

## Customer Portal

By default, when the origin returns a redirect, Gcore caches and serves that redirect, while **Redirection from origin** makes the CDN follow selected codes, fetch the target, and cache it instead.

## Redirection from origin

Turn the option on for the redirect codes the CDN should follow.

**1. Open the CDN resource**

In the [Gcore Customer Portal](https://portal.gcore.com), navigate to **CDN** and select the CDN resource.

![CDN resources list](https://mintcdn.com/gcore/B2P8Bp_OcOlF2xTP/images/docs/cdn/cdn-resource-options/cache/set-up-a-cdn-resource-to-follow-origin-redirects/cdn-resources-list.png?fit=max\&auto=format\&n=B2P8Bp_OcOlF2xTP\&q=85\&s=4ae2c7087cb19037dcc575561ef2417b)

**2. Open redirection from origin**

In the sidebar, under the **Cache** section, select **Redirection from origin**.

![CDN resource settings with Cache section open](https://mintcdn.com/gcore/B2P8Bp_OcOlF2xTP/images/docs/cdn/cdn-resource-options/cache/set-up-a-cdn-resource-to-follow-origin-redirects/cdn-resource-settings-cache.png?fit=max\&auto=format\&n=B2P8Bp_OcOlF2xTP\&q=85\&s=349a489d415ae9973ffc61eed2f15e89)

**3. Enable origin redirects**

Enable the **Follow redirect from origin** toggle.

![Follow redirect from origin toggle enabled with status codes field](https://mintcdn.com/gcore/B2P8Bp_OcOlF2xTP/images/docs/cdn/cdn-resource-options/cache/set-up-a-cdn-resource-to-follow-origin-redirects/follow-redirect-enabled.png?fit=max\&auto=format\&n=B2P8Bp_OcOlF2xTP\&q=85\&s=290cd07873b700829fe0fded45d03141)

**4. Select redirect codes**

In the **Choose redirect status codes** field, select one or more codes: 301, 302, 303, 307, or 308. Code 302 is selected by default.

![Choose redirect status codes dropdown](https://mintcdn.com/gcore/B2P8Bp_OcOlF2xTP/images/docs/cdn/cdn-resource-options/cache/set-up-a-cdn-resource-to-follow-origin-redirects/redirect-status-codes-dropdown.png?fit=max\&auto=format\&n=B2P8Bp_OcOlF2xTP\&q=85\&s=104064b839964bc1c36fd292c521e8c4)

**5. Save the resource**

Click **Save changes**. Allow at least 15 minutes for the change to take effect.

> [!WARNING]
>
> Changing this option can leave a previously cached redirect or target response in cache. [Purge the cache](https://docs.gcore.com/cdn/clear-cdn-resource-cache-by-url-pattern-or-all.md) after saving.

## REST API

When `follow_origin_redirect` is enabled for a selected status code, the CDN follows it, caches the target response, and serves that content instead of the redirect itself.

> [!NOTE]
>
> An [API token](https://docs.gcore.com/account-settings/api-tokens.md) is required.

Export these variables. `CDN_RESOURCE_ID` is the `id` from [list CDN resources](https://docs.gcore.com/api-reference/cdn/cdn-resources/list-cdn-resources).

```bash theme={null}
export GCORE_API_KEY="{YOUR_API_KEY}"
export CDN_RESOURCE_ID="{YOUR_RESOURCE_ID}"
```

## Origin redirect option

`codes` accepts 301, 302, 303, 307, 308, and 404. The Customer Portal lists 301, 302, 303, 307, and 308.

`enabled` and `codes` are both required, because an empty `codes` list returns HTTP 400.

> [!WARNING]
>
> Changing this option can leave a previously cached redirect or target response in cache. [Purge the cache](https://docs.gcore.com/cdn/clear-cdn-resource-cache-by-url-pattern-or-all.md) after the update, and allow at least 15 minutes for the change to take effect.

Update the resource with `follow_origin_redirect` enabled for code 302.

> [tab] Python SDK
>
> ```python theme={null}
> import os
> from gcore import Gcore
>
> client = Gcore()
>
> resource = client.cdn.cdn_resources.update(
>     int(os.environ["CDN_RESOURCE_ID"]),
>     options={
>         "follow_origin_redirect": {"enabled": True, "codes": [302]},
>     },
> )
> print(resource.options.follow_origin_redirect.enabled, resource.options.follow_origin_redirect.codes)
> ```

> [tab] Go SDK
>
> ```go theme={null}
> package main
>
> import (
>     "context"
>     "fmt"
>     "os"
>     "strconv"
>
>     gcore "github.com/G-Core/gcore-go"
>     "github.com/G-Core/gcore-go/cdn"
> )
>
> func main() {
>     client := gcore.NewClient()
>     ctx := context.Background()
>     resourceID, err := strconv.ParseInt(os.Getenv("CDN_RESOURCE_ID"), 10, 64)
>     if err != nil {
>         panic(err)
>     }
>
>     resource, err := client.CDN.CDNResources.Update(ctx, resourceID, cdn.CDNResourceUpdateParams{
>         Options: cdn.CDNResourceUpdateParamsOptions{
>             FollowOriginRedirect: cdn.CDNResourceUpdateParamsOptionsFollowOriginRedirect{
>                 Enabled: true,
>                 Codes:   []int64{302},
>             },
>         },
>     })
>     if err != nil {
>         panic(err)
>     }
>     fmt.Println(resource.Options.FollowOriginRedirect.Enabled, resource.Options.FollowOriginRedirect.Codes)
> }
> ```

> [tab] curl
>
> ```bash theme={null}
> curl -X PATCH "https://api.gcore.com/cdn/resources/$CDN_RESOURCE_ID" \
>   -H "Authorization: APIKey $GCORE_API_KEY" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "options": {
>       "follow_origin_redirect": {
>         "enabled": true,
>         "codes": [302]
>       }
>     }
>   }'
> ```
>
> The API returns HTTP 200:
>
> ```json theme={null}
> {
>   "id": 12345,
>   "options": {
>     "follow_origin_redirect": {
>       "enabled": true,
>       "codes": [302]
>     }
>   }
> }
> ```

## Terraform

Set `follow_origin_redirect` so the CDN follows selected origin redirect codes, fetches the target, and caches that response instead of the redirect, using the [Terraform provider](https://docs.gcore.com/developer-tools/terraform/overview.md) v2. By default the CDN caches and serves the redirect itself.

`codes` accepts 301, 302, 303, 307, 308, and 404. The Customer Portal lists 301, 302, 303, 307, and 308, and 302 is selected by default. `enabled` and `codes` are both required. An empty `codes` list returns HTTP 400. Leaving `follow_origin_redirect` out of `options` leaves the stored option unchanged. Run `terraform apply` after editing `options`, and allow at least 15 minutes for the change to take effect.

> [!WARNING]
>
> Changing this option can leave a previously cached redirect or target response in cache. [Purge the cache](https://docs.gcore.com/cdn/clear-cdn-resource-cache-by-url-pattern-or-all.md) after the update.

## Follow code 302

Set `enabled` to true and `codes` to 302. The CDN follows that redirect and caches the target response.

```hcl theme={null}
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"

  options = {
    follow_origin_redirect = {
      enabled = true
      codes   = [302]
    }
  }
}
```
