Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-netlify): Allow status codes in redirects (#11255) #11484

Merged
merged 7 commits into from
Feb 5, 2019
9 changes: 7 additions & 2 deletions packages/gatsby-plugin-netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,20 @@ You can validate the `_headers` config through the

You can create redirects using the [`createRedirect`](https://www.gatsbyjs.org/docs/actions/#createRedirect) action.

In addition to the options provided by the Gatsby API, you can pass these options specific to this plugin:

| Attribute | Description |
| --------- | ----------- |
| `force` | Overrides existing content in the path. This is particularly useful for domain alias redirects. See [the Netlify documentation for this option](https://www.netlify.com/docs/redirects/#structured-configuration). |
| `statusCode` | Overrides the HTTP status code whih is `302` by default, or `301` when [`isPermanent`](https://www.gatsbyjs.org/docs/actions/#createRedirect) is `true`. Since Netlify supports more status codes, this allows you to set a different status code. Like `200` for a rewrite, or `404` for a custom error page. See [the Netlify documentation for this option](https://www.netlify.com/docs/redirects/#http-status-codes). |
joostdecock marked this conversation as resolved.
Show resolved Hide resolved
joostdecock marked this conversation as resolved.
Show resolved Hide resolved

An example:

```javascript
createRedirect({ fromPath: "/old-url", toPath: "/new-url", isPermanent: true })
createRedirect({ fromPath: "/url", toPath: "/zn-CH/url", Language: "zn" })
```

> NOTE: You can pass the `force` option to override existing content in the path. This is particularly useful for domain alias redirects. See the Netlify documentation on this option [here](https://www.netlify.com/docs/redirects/#structured-configuration).

You can also create a `_redirects` file in the `static` folder for the same effect. Any programmatically created redirects will be appended to the file.

```sh
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-plugin-netlify/src/create-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export default async function writeRedirectsFile(
redirectInBrowser, // eslint-disable-line no-unused-vars
force,
toPath,
statusCode, // eslint-disable-line no-unused-vars
joostdecock marked this conversation as resolved.
Show resolved Hide resolved
...rest
} = redirect

let status = isPermanent ? `301` : `302`
if (statusCode) status = statusCode

if (force) status = status.concat(`!`)

Expand Down