|
1 | 1 | --- |
2 | 2 | pcx_content_type: concept |
3 | 3 | title: Redirects |
4 | | - |
5 | 4 | --- |
6 | 5 |
|
7 | | -import { Render } from "~/components" |
8 | | - |
9 | | -To use redirects on Cloudflare Pages, declare your redirects in a plain text file called `_redirects` without a file extension, in the output folder of your project. The [build output folder](/pages/configuration/build-configuration/) is project-specific, so the `_redirects` file should not always be in the root directory of the repository. Changes to redirects will be updated to your website at build time so make sure you commit and push the file to trigger a new build each time you update redirects. |
10 | | - |
11 | | -:::caution |
12 | | - |
13 | | - |
14 | | -Redirects defined in the `_redirects` file are not applied to requests served by [Functions](/pages/functions/), even if the Function route matches the URL pattern. If your Pages application uses Functions, you must migrate any behaviors from the `_redirects` file to the code in the appropriate `/functions` route, or [exclude the route from Functions](/pages/functions/routing/#create-a-_routesjson-file). |
15 | | - |
16 | | - |
17 | | -::: |
18 | | - |
19 | | -## Structure |
20 | | - |
21 | | -### Per line |
22 | | - |
23 | | -Only one redirect can be defined per line and must follow this format, otherwise it will be ignored. |
24 | | - |
25 | | -```txt |
26 | | -[source] [destination] [code?] |
27 | | -``` |
28 | | - |
29 | | - |
30 | | - |
31 | | -* `source` required |
32 | | - |
33 | | - * A file path. |
34 | | - * Can include [wildcards (`*`)](#splats) and [placeholders](#placeholders). |
35 | | - * Because fragments are evaluated by your browser and not Cloudflare's network, any fragments in the source are not evaluated. |
36 | | -* `destination` required |
37 | | - |
38 | | - * A file path or external link. |
39 | | - * Can include fragments, query strings, [splats](#splats), and [placeholders](#placeholders). |
40 | | -* `code` default: `302` |
41 | | - |
42 | | - * Optional parameter |
43 | | - |
44 | | - |
45 | | - |
46 | | -Lines starting with a `#` will be treated as comments. |
47 | | - |
48 | | -### Per file |
49 | | - |
50 | | -A project is limited to 2,000 static redirects and 100 dynamic redirects, for a combined total of 2,100 redirects. Each redirect declaration has a 1,000-character limit. |
51 | | - |
52 | | -In your `_redirects` file: |
53 | | - |
54 | | -* The order of your redirects matter. If there are multiple redirects for the same `source` path, the topmost redirect is applied. |
55 | | -* Static redirects should appear before dynamic redirects. |
56 | | -* Redirects are always followed, regardless of whether or not an asset matches the incoming request. |
57 | | - |
58 | | -A complete example with multiple redirects may look like the following: |
59 | | - |
60 | | -```txt |
61 | | -/home301 / 301 |
62 | | -/home302 / 302 |
63 | | -/querystrings /?query=string 301 |
64 | | -/twitch https://twitch.tv |
65 | | -/trailing /trailing/ 301 |
66 | | -/notrailing/ /nottrailing 301 |
67 | | -/page/ /page2/#fragment 301 |
68 | | -/blog/* https://blog.my.domain/:splat |
69 | | -/products/:code/:name /products?code=:code&name=:name |
70 | | -``` |
71 | | - |
72 | | -:::note |
73 | | - |
74 | | - |
75 | | -In the case of some frameworks, such as Jekyll, you may need to manually copy and paste your `_redirects` file to the build output directory. To do this: |
76 | | - |
77 | | -1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account. |
78 | | -2. Go to **Workers & Pages** > your Pages project > **Settings** > **Builds & deployments**. |
79 | | -3. Go to **Build configurations** > **Edit configurations** > change the build command to `jekyll build && cp _redirects _site/_redirects` and select **Save**. |
80 | | - |
81 | | - |
82 | | -::: |
83 | | - |
84 | | -## Advanced redirects |
85 | | - |
86 | | -Cloudflare currently offers limited support for advanced redirects. More support will be added in the future. |
87 | | - |
88 | | - |
89 | | - |
90 | | -| Feature | Support | Example | Notes | |
91 | | -| ----------------------------------- | ------- | --------------------------------------------------------------- | --------------------------------------- | |
92 | | -| Redirects (301, 302, 303, 307, 308) | Yes | `/home / 301` | 302 is used as the default status code. | |
93 | | -| Rewrites (other status codes) | No | `/blog/* /blog/404.html 404` | | |
94 | | -| Splats | Yes | `/blog/* /posts/:splat` | Refer to [Splats](#splats). | |
95 | | -| Placeholders | Yes | `/blog/:year/:month/:date/:slug /news/:year/:month/:date/:slug` | Refer to [Placeholders](#placeholders). | |
96 | | -| Query Parameters | No | `/shop id=:id /blog/:id 301` | | |
97 | | -| Proxying | Yes | `/blog/* /news/:splat 200` | Refer to [Proxying](#proxying). | |
98 | | -| Domain-level redirects | No | `workers.example.com/* workers.example.com/blog/:splat 301` | | |
99 | | -| Redirect by country or language | No | `/ /us 302 Country=us` | | |
100 | | -| Redirect by cookie | No | `/\* /preview/:splat 302 Cookie=preview` | | |
101 | | - |
102 | | - |
103 | | - |
104 | | -## Redirects and header matching |
105 | | - |
106 | | -Redirects execute before headers, so in the case of a request matching rules in both files, the redirect will win out. |
107 | | - |
108 | | -### Splats |
109 | | - |
110 | | -On matching, a splat (asterisk, `*`) will greedily match all characters. You may only include a single splat in the URL. |
111 | | - |
112 | | -The matched value can be used in the redirect location with `:splat`. |
113 | | - |
114 | | -### Placeholders |
115 | | - |
116 | | -<Render file="headers_redirects_placeholders" params={{ one: "redirect" }} /> |
117 | | - |
118 | | -```txt |
119 | | -/movies/:title /media/:title |
120 | | -``` |
121 | | - |
122 | | -### Proxying |
123 | | - |
124 | | -Proxying will only support relative URLs on your site. You cannot proxy external domains. |
125 | | - |
126 | | -Only the first redirect in your will apply. For example, in the following example, a request to `/a` will render `/b`, and a request to `/b` will render `/c`, but `/a` will not render `/c`. |
127 | | - |
128 | | -``` |
129 | | -/a /b 200 |
130 | | -/b /c 200 |
131 | | -``` |
132 | | - |
133 | | -:::note |
134 | | - |
135 | | -Be aware that proxying pages can have an adverse effect on search engine optimization (SEO). Search engines often penalize websites that serve duplicate content. Consider adding a `Link` HTTP header which informs search engines of the canonical source of content. |
136 | | - |
137 | | -For example, if you have added `/about/faq/* /about/faqs 200` to your `_redirects` file, you may want to add the following to your `_headers` file: |
138 | | - |
139 | | -```txt |
140 | | -/about/faq/* |
141 | | - Link: </about/faqs>; rel="canonical" |
142 | | -``` |
143 | | - |
144 | | - |
145 | | -::: |
146 | | - |
147 | | -## Surpass `_redirects` limits |
148 | | - |
149 | | -A [`_redirects`](/pages/platform/limits/#redirects) file has a maximum of 2,000 static redirects and 100 dynamic redirects, for a combined total of 2,100 redirects. Use [Bulk Redirects](/rules/url-forwarding/bulk-redirects/) to handle redirects that surpasses the 2,100 redirect rules limit set by Pages. |
150 | | - |
151 | | -:::note |
152 | | - |
153 | | - |
154 | | -The redirects defined in the `_redirects` file of your build folder can work together with your Bulk Redirects. In case of duplicates, Bulk Redirects will run in front of your Pages project, where your other redirects live. |
155 | | - |
156 | | -For example, if you have Bulk Redirects set up to direct `abc.com` to `xyz.com` but also have `_redirects` set up to direct `xyz.com` to `foo.com`, a request for `abc.com` will eventually redirect to `foo.com`. |
157 | | - |
158 | | - |
159 | | -::: |
160 | | - |
161 | | -To use Bulk Redirects, refer to the [Bulk Redirects dashboard documentation](/rules/url-forwarding/bulk-redirects/create-dashboard/) or the [Bulk Redirects API documentation](/rules/url-forwarding/bulk-redirects/create-api/). |
162 | | - |
163 | | -## Related resources |
| 6 | +import { Render } from "~/components"; |
164 | 7 |
|
165 | | -* [Transform Rules](/rules/transform/) |
| 8 | +<Render product="workers" file="redirects" params={{ product: "pages" }} /> |
0 commit comments