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

Redirect Status Code is always 302. Is it possible to create redirect to external url? #298

Open
ratkorle opened this issue Jan 20, 2022 · 0 comments

Comments

@ratkorle
Copy link

Hi all, can anyone help me with redirects, I have AWS S3 hosted website and I am using gatsby-plugin-s3.
I am getting the redirects data from Sanity and then I am creating redirects using createRedirect() inside gatsby-node.js:
However the StatusCode generated in .cache/s3.routingRules.json is always 302.

I would like to achieve:

  1. 301 and 302 redirects
  2. Redirect to external url : mysite.com/test -> google.com

Example redirect data:

[
 {
  _id: 'b6b1a54e-9642-4509-a691-9b183d15b681',
  _createdAt: '2022-01-18T15:25:57Z',
  fromPath: '/test/',
  statusCode: '301',
  toPath: '/services/'
},
{
  _id: 'f8f1c364-0305-4848-9f89-750d8eaadcee',
  _createdAt: '2022-01-19T13:49:56Z',
  fromPath: '/test2/',
  statusCode: '302',
  toPath: 'www.google.com'
} 
]

Here is how I create redirects in gatsby-node.js:

 if (redirects && redirects.length > 0) {
    redirects.forEach((redirect) => {
      createRedirect({
        fromPath: redirect.fromPath,
        toPath: redirect.toPath,
        statusCode: parseInt(redirect.statusCode),
        force: true,
      });
    });
  }

This how the gatsby-plugin-s3 is configured in gatsby-config.js (listed first in the plugins list):

 {
      resolve: `gatsby-plugin-s3`,
      options: {
        bucketName: `client-bucket-${process.env.GATSBY_STAGE}-${process.env.GATSBY_LANG}`,
        region: "eu-west-1",
        customAwsEndpointHostname: "s3.eu-west-1.amazonaws.com",
        generateRoutingRules: true,
        generateRedirectObjectsForPermanentRedirects: true,
      },
    },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant