> ## 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.

# Redirect a CDN hostname to another domain

> Redirect every request from a CDN hostname to another host and keep the path and query string.

## Customer Portal

Gcore CDN can redirect requests from an existing CDN hostname to another domain while preserving the original path and query string. This allows you to move a website to a new domain without breaking existing links.

The redirect is configured on a [CDN resource](https://docs.gcore.com/cdn/getting-started/create-a-cdn-resource/create-a-cdn-resource.md), which still requires an origin group. Gcore CDN returns the redirect directly without fetching content from the origin.

## Send the visitor to another host

The **Rewrite body** field is prefilled with `/(.*) /$1`. Saving that value changes the origin path and does not send the visitor to another host.

1\. Log in to the [Gcore Customer Portal](https://portal.gcore.com) and navigate to **CDN** > **CDN resources**. Open the resource.

![CDN in the sidebar and the opened resource on the OPTIONS tab](https://mintcdn.com/gcore/Hlp6LQ7yfVcQnenn/images/docs/cdn/cdn-resource-options/redirect-one-domain-to-another/redirect-one-domain-to-another-image1.png?fit=max\&auto=format\&n=Hlp6LQ7yfVcQnenn\&q=85\&s=3cdc3ce66588131e5ecbeb1b8d49bd9f)

2\. On the **OPTIONS** tab, select **Content** > **Rewrite**.

3\. Turn on **Enable rewrite**.

4\. In the **Rewrite body** field, enter the pattern and the target URL, separated by a space. Replace `example.com` with the host that should receive the visitor:

```
^/(.*)$ https://example.com/$1
```

`(.*)` captures the path after the first slash, and `$1` inserts that capture.

5\. In the **Rewrite flag** dropdown, select **permanent** for HTTP 301 or **redirect** for HTTP 302. An absolute replacement URL also returns HTTP 302 with **break** or **last**.

6\. Click **Save changes**.

![Content menu with Rewrite selected, the domain pattern in Rewrite body, and the permanent flag](https://mintcdn.com/gcore/Hlp6LQ7yfVcQnenn/images/docs/cdn/cdn-resource-options/redirect-one-domain-to-another/redirect-one-domain-to-another-image2.png?fit=max\&auto=format\&n=Hlp6LQ7yfVcQnenn\&q=85\&s=f1ceb4addb7217f881a6938d0005b138)

These requests return 301:

| Request URL | Target URL |
| - | - |
| `http://cdn.example.com/` | `https://example.com/` |
| `http://cdn.example.com/admin/?logged_out=true` | `https://example.com/admin/?logged_out=true` |
| `http://cdn.example.com/docs/page?x=1` | `https://example.com/docs/page?x=1` |

7\. Confirm the redirect. Replace `cdn.example.com` with the CDN hostname:

```bash theme={null}
curl -I "http://cdn.example.com/docs/page?x=1"
```

The response is 301, and the `Location` header is `https://example.com/docs/page?x=1`.

## Pick another target

The same option covers two narrower targets. Keep **permanent** or **redirect** as in the steps above.

* **One URL**: `^/.*$ https://example.com/`. Every path redirects to `https://example.com/`, and the query string is kept. `^/(.*)$ https://example.com/` is rejected because the capture is never inserted.
* **Homepage only**: `^/$ https://example.com/`. A request for `/` redirects to `https://example.com/`. Any other path stays on the origin.

## REST API

A CDN hostname returns a redirect to another host and keeps the request path. The update sets `options.rewrite` on an existing [CDN resource](https://docs.gcore.com/cdn/getting-started/create-a-cdn-resource/create-a-cdn-resource.md).

The resource still needs an origin group. The redirect comes from the CDN, so the request is not fetched from the origin.

> [!NOTE]
>
> An [API token](https://docs.gcore.com/account-settings/api-tokens.md) is required. The CDN resource ID is the `id` from the [resources list](https://docs.gcore.com/api-reference/cdn/cdn-resources/list-cdn-resources) call.

Export these variables:

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

## Redirect and keep the path

`body` is a regular expression and an absolute URL, separated by a space. `$1` inserts the captured path. `flag` `permanent` returns 301, and `flag` `redirect` returns 302.

A request for `/docs/page?x=1` returns `Location: https://example.com/docs/page?x=1`. A replacement that starts with `https://` also redirects when `flag` is `break`, `last`, or omitted. An omitted flag is stored as `break` and returns 302.

> [tab] Python SDK
>
> ```python theme={null}
> import os
>
> from gcore import Gcore
>
> client = Gcore()
> resource_id = int(os.environ["CDN_RESOURCE_ID"])
>
> updated = client.cdn.cdn_resources.update(
>     resource_id,
>     options={
>         "rewrite": {
>             "enabled": True,
>             "body": "^/(.*)$ https://example.com/$1",
>             "flag": "permanent",
>         }
>     },
> )
> print(updated.options.rewrite.enabled, updated.options.rewrite.body, updated.options.rewrite.flag)
> ```

> [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)
>     }
>
>     updated, err := client.CDN.CDNResources.Update(ctx, resourceID, cdn.CDNResourceUpdateParams{
>         Options: cdn.CDNResourceUpdateParamsOptions{
>             Rewrite: cdn.CDNResourceUpdateParamsOptionsRewrite{
>                 Enabled: true,
>                 Body:    "^/(.*)$ https://example.com/$1",
>                 Flag:    "permanent",
>             },
>         },
>     })
>     if err != nil {
>         panic(err)
>     }
>     fmt.Println(updated.Options.Rewrite.Enabled, updated.Options.Rewrite.Body, updated.Options.Rewrite.Flag)
> }
> ```

> [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": {
>       "rewrite": {
>         "enabled": true,
>         "body": "^/(.*)$ https://example.com/$1",
>         "flag": "permanent"
>       }
>     }
>   }'
> ```
>
> The API returns HTTP 200. The saved option is:
>
> ```json theme={null}
> {
>   "enabled": true,
>   "body": "^/(.*)$ https://example.com/$1",
>   "flag": "permanent"
> }
> ```

## Redirect every path to one URL

A pattern with no capture group sends every path to one URL and keeps the query string. `^/(.*)$ https://example.com/` returns HTTP 400 because the capture is never inserted.

Send the same update with this `body` and `flag` `permanent`. A request for `/docs/page?x=1` returns `Location: https://example.com/?x=1`.

```
^/.*$ https://example.com/
```

## Redirect only the homepage

A pattern that matches only `/` redirects the homepage. Any other path is not redirected.

Send the same update with this `body` and `flag` `permanent`. A request for `/` returns `Location: https://example.com/`.

```
^/$ https://example.com/
```

## Terraform

A CDN hostname returns a 301 redirect to another host and keeps the request path. The [Terraform provider](https://docs.gcore.com/developer-tools/terraform/overview.md) stores the pattern in the `rewrite` block of `gcore_cdn_resource`. Run `terraform apply` after editing `options`.

The resource still needs an origin group. The redirect comes from the CDN, so the request is not fetched from the origin. `flag` `permanent` returns 301, and `flag` `redirect` returns 302.

```hcl theme={null}
resource "gcore_cdn_origin_group" "example" {
  name = "my-origin-group"

  sources = [
    {
      source  = "origin.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 = {
    rewrite = {
      enabled = true
      body    = "^/(.*)$ https://example.com/$1"
      flag    = "permanent"
    }
  }
}
```

`$1` inserts the path captured by `(.*)`. A request for `/docs/page?x=1` returns `Location: https://example.com/docs/page?x=1`.

To send every path to one URL, set `body` to `^/.*$ https://example.com/`. A pattern that captures a group and never inserts it is rejected. To redirect only `/`, set `body` to `^/$ https://example.com/`.
