I’m migrating a WordPress site to a static site hosted in an Amazon s3 bucket. All pages are in a directory structure (eg, blog/index.html) but can be accessed via /blog/.
By default, Amazon redirects urls missing trailing slashes via a 302 redirect (www.site.com/page — 302 –> www.site.com/page/). This is awful for SEO, and I’m trying to make these redirects 301s.
I’ve tried adding page.html objects in the root directory and setting up a redirect to /page/ per Amazon’s instructions. This only creates 301s for /page.html. /page still causes a 302.
Amazon’s redirect instructions:
http://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html
Relevant blog post on issue:
http://moz.com/community/q/new-static-site-with-302s
Has anyone encountered this, or have any ideas what I can do?
2
Answers
According to the docs, it is indeed their design to return a 302:
Going by the letter of their documentation, try renaming your
page.html
objects to simply be calledpage
(without the .html extension). It sucks that you have to do this for every URL containing a trailing slash, but we’re stuck with it until Amazon lets us configure the actual redirect code for this behavior.You can fix this problem now using CloudFront and Lambda@Edge. You can intercept the request coming back from S3 using the ‘origin response’ event and then change the response status-code to 301.
Below is the code you need to add to your Lambda handler.
Here’s a blog post, I wrote, that explains how to do this in detail
https://www.vividbytes.io/fixing-redirect-codes-on-static-s3-websites/