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

# Rewrite: redirect requests from the CDN to the origin

The Rewrite option changes or redirects request paths before the CDN fetches content from the origin. It operates according to [nginx](https://nginx.org/en/docs/http/ngx_http_rewrite_module.html) rewrite rules.

To enable the option, open a CDN resource, navigate to the **Options** tab, then to the **Content** section. Toggle **Enable rewrite**. The same setting is available in the **Rules** section.

<Frame>
  <img src="https://mintcdn.com/gcore-doc-1894/9U1xXa0egFyWElxm/images/docs/cdn/cdn-resource-options/rewrite-redirect-requests-from-the-cdn-to-the-origin/rewrite-redirect-requests-from-the-cdn-to-the-origin-image1.png?fit=max&auto=format&n=9U1xXa0egFyWElxm&q=85&s=801157a88fc6fb7d5efc494b53538113" alt="CDN resource Options tab with Rewrite enabled, showing the Rewrite body field and Rewrite flag dropdown" width="553" height="313" data-path="images/docs/cdn/cdn-resource-options/rewrite-redirect-requests-from-the-cdn-to-the-origin/rewrite-redirect-requests-from-the-cdn-to-the-origin-image1.png" />
</Frame>

The **Rewrite body** field pre-fills with `/(.*) /$1` as an example. Replace it with two space-separated directives using regular expressions:

1. The path the Rewrite applies to.
2. The replacement path.

To rewrite all request paths to the `/media/` directory, use:

```
/(.*) /media/$1
```

where:

* **`/(.*)`** — matches all paths after `/`. The `(.*)` group captures everything that follows the slash.
* **`/media/`** — the directory on the origin where CDN servers look for content.
* **`$1`** — references the first capture group. Use `$2` and `$3` for the second and third groups.

In the **Rewrite flag** dropdown, select the nginx flag that controls how the rewrite is applied:

* **break** (default) — stops processing further rewrite rules after this one.
* **last** — stops processing rewrite rules and starts a new location search.
* **redirect** — returns a 302 temporary redirect to the client.
* **permanent** — returns a 301 permanent redirect to the client.
