Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS s3 forces 302 redirects when url has no trailing slash - need 301s

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?

like image 491
nrlr Avatar asked May 07 '15 01:05

nrlr


2 Answers

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/

like image 173
vividbytes Avatar answered Sep 21 '22 10:09

vividbytes


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.

like image 20
Brian Nguyen Avatar answered Sep 20 '22 10:09

Brian Nguyen