gatsby-plugin-s3: 301 Redirect
Hello š
I am trying to apply 301 redirect with my Gatsby and S3 + Cloudfront , and so far i have no success , i have about 204 redirect url , i will list below my configuration .
fromPath: `/old-url`,
toPath: `/new-url`,
isPermanent: true,
redirectInBrowser: true,
});
`
{
resolve: 'gatsby-plugin-s3',
options: {
bucketName: process.env.S3_BUCKET,
region: 'eu-west-1',
protocol: 'https',
hostname: 'stage-mydomain.de',
bucketPrefix: 'de',
generateRoutingRules: true,
generateRedirectObjectsForPermanentRedirects: true,
}
}
note that i have no plugin related to redirect inside my config ,
what i would like to accomplish is pure 301 server side redirect , i dont want JS redirect and Meta redirect because
Js will not work with JS disabled and Meta is not good for SEO .
can someone help please?
Thanks
>
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 37
Creating a fork duplicates the project into a repository that you own. You can change that freely. If we make updates to our version you can pull those into your repository, and if you make changes that you think weād like you can submit a Pull Request to us.
In your websiteās package.json you have a line that looks something like this:
If your fork is hosted on GitHub, you can alter this to point to your fork by doing something like:
You can find more details here:
Best of luck š
302 redirects in S3 Static Website Hosting are usually to add a trailing slash. If you add a trailing slash to your redirectās
toPathit should only need a single 301 redirect.Thanks for the offer to buy me a beer. We donāt have any mechanism set up for tipping, and Iām just happy that itās working for you.
Fixing the bug permanently should be fairly straightforward, but first I want guidance from the Gatsby Core team on how they think redirects should be handled. Unfortunately they recently restructured their community, and now itās harder to get in touch with them. I might try resubmitting my question as a bug instead of a discussion to see if that gets better visibility.
At the moment Iām thinking the best solution would be for us to subtract
pathPrefixfrom the beginning of thefromPathof all redirects, then to addbucketPrefix. This is basically just a workaround for what I consider to be a bug in Gatsby.gatsbyjs/gatsby#27988
Thanks a lot ! i will check the documentation u mentioned and update you with the result tomorrow ^_^ thanks again for your time , good night
it working !!! i removed my lambda edges for Cache and add index.html as you once told me here https://github.com/jariz/gatsby-plugin-s3/issues/208#issuecomment-725027672 and now its working !!!
it was redirected in 2 stages from 301 to 302 then to 200, no idea why š but i am now super happy its working .
i want to thank you very much for your time and effort , i would like to tip you or buy you a beer š please let me know how can i do that.
also would like to know what is the next step to fix this bug so i can switch back to your version and removed my Temp NPM .
Thanks a lot
Check out the āMake a CloudFront Distributionā sectin of this guide. They added the origin while creating the distribution, but you can do the same thing when adding an origin to an existing distribution. Basically you just need to put the S3 Static Website Hosting endpoint in the Origin Domain Name field.
Good question, it was late at night when I gave my last reply and I didnāt think it through properly. 3 options:
Publish your fork to npm. Then in your website, change package.json to say
"ahmadkhalaf1/gatsby-plugin-s3": "^0.3.8"Make a branch in your fork called ābuildā or ājavascriptā. Remove *.js and *.d.ts from .gitignore on this branch. On your website, change package.json to say
"gatsby-plugin-s3": "ahmadkhalaf1/gatsby-plugin-s3#build"On your CI, after youāve run
npm cifor your project, run:(That last one might or might not work, Iāve had issues before with npm in nested projects)
It seems from here and here that maybe Gatsby itself is prefixing the paths. Iāll try to raise this with the Gatsby Core team and see what they advise. This is unlikely to be a quick fix sorry, but I want to make sure we do this right.
In the meantime, your best option might be to create a private fork and remove these lines.
@JoshuaWalsh ok i guess i find out why its not working , and i think ( not so sure ) that there is a bug that does not handle pathPrefix with createRedirect in Gatsby plugin s3 .
so in short , here what i could find out in my config i have
so my main domain is ,
https:/www.mydomain.de/de/index-page-title/slug-titlefor example .so after my build is done , my s3 bucket contain 1 main folder
deand all my build files are inside this folder.create redirect
now at the final step , when i do
npx -n \"-r dotenv/config\" gatsby-plugin-s3 deploy --yesall my build files are copied under de folder in my s3 bucket , but what i find out that all redirect objects are copied under another /de folder inside the main /de folder which is wrong and i am kind of sure that this is the problem .
as u see the problem is here ,de/de , this is why i assume its an issue with this step of Gatsby Plugin S3.
and this is a picture how it look like inside my S3 Bucket
this is the main DE folder
and this is what we see inside the de folder
now inside the second de folder i can find folders with (index-title) name , and inside it there is an object with meta data x-amz-website-redirect-location pointing to the right redirect url . but its an object not a folder with index.html inside , is this how it should be?
Thanks and sorry for the long post š i hope we can solve this before my boss get more angry š