skip to Main Content

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


  1. According to the docs, it is indeed their design to return a 302:

    … if you exclude the trailing slash from the preceding URL, Amazon S3 first looks for an object photos in the bucket. If the photos object is not found, then it searches for an index document, photos/index.html. If that document is found, Amazon S3 returns a 302 Found message and points to the photos/ key. For subsequent requests to photos/, Amazon S3 returns photos/index.html.”

    Going by the letter of their documentation, try renaming your page.html objects to simply be called page (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.

    Login or Signup to reply.
  2. 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.

    exports.handler = async (event) => {
        const response = event.Records[0].cf.response;
        if (response.status === '302') {
            response.status = '301';
            response.statusDescription = 'Moved Permanently'
        }
        return response;
    };
    

    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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search