Gatsby: [Question] Redirecting a path to different domain?

Created on 30 Jan 2018  ·  7Comments  ·  Source: gatsbyjs/gatsby

Question

Is there a way to create a redirect for external domain? createRedirect works for pushing a user just from path to path. I'm looking for:

myservice.com/doiqualify -> https://myservice.typeform.com/qw/2i42ori

I noticed createRedirect just appends the url to the root like myservice.com/https://myservice.typeform...

My environment

Deploying to S3 bucket and using Cloudfront (using Route 53 if that matters)

How I'm Doing It Now

I have a redirect template that's blank which runs some javascript checking for the path and then assigns a new URL.

_Snippet from my gatsby-node.js_

  // Create Redirect Pages....
  const redirectPage = path.resolve("src/templates/redirect.jsx");
  redirectsBatch.forEach(({ from }) => {
    createPage({
      path: from,
      component: redirectPage,
    });
  });

_code snippet in the redirect.jsx template that cycles through the same redirect JSON and assigns the new href_

...
    redirectBatch.forEach((re) => {
      if (window.location.pathname.includes(re.from)) window.location.href = re.to;
    });
...

Should I be doing it that way?

This feels a little wonky, so I'm not sure if this is the right way to go about it. Is there an method or plugin I should be using instead?

question or discussion

Most helpful comment

I'd love for this issue to be reopened please––it would be great if createRedirect could handle external redirects.

All 7 comments

Hmmm this looks like a bug. I'm not sure we considered redirecting to other domains when designing the createRedirect API. Could you look into creating a PR with a fix?

Sure thing! 👍

Needed exactly this functionality of having an internal path redirect to some other domain, cool trick @youmustfight

Was a PR ever submitted for this?

+1 Would be neat if createRedirect worked for external urls as well. Neither window.location.replace(url) or window.location = url works for me on Netlify for some reason...

Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help!

I'd love for this issue to be reopened please––it would be great if createRedirect could handle external redirects.

For anyone interested to redirect with wildcard, you just need to ensure you have the correct host config (netlify/apache. Gatsby does not really filter it out so it gives us some freedom.

For example:

createRedirect({
  fromPath: "/someLegacyPath/*",
  toPath: "https://newDomain.com/someLegacyPath/:splat",
})

:splat is something specific for Netlify redirects. (see https://www.netlify.com/docs/redirects/#splats)
Your redirect won't be "portable" but at least it will work in Netlify.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KyleAMathews picture KyleAMathews  ·  3Comments

totsteps picture totsteps  ·  3Comments

timbrandin picture timbrandin  ·  3Comments

dustinhorton picture dustinhorton  ·  3Comments

Oppenheimer1 picture Oppenheimer1  ·  3Comments