Skip to content

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

@ratkorle

Description

@ratkorle

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,
      },
    },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions