Is it possible to put a redirect header on a s3-object? Like a 301 redirect.

For example:

mybucket.amazon-aws.com/myobject --> example.com/test

Preferably by setting a header like this on the object:

HTTP/1.1 301 Moved Permanently
Location: http://example.com/test
Content-Type: text/html
Content-Length: 0

Within the last month, this functionality has just been added.

You can find the API documentation here:

http://docs.amazonwebservices.com/AmazonS3/latest/dev/how-to-page-redirect.html

When you PUT your object, you need to set the x-amz-website-redirect-location key against that object to the 301 redirect that you wish to use.

You can also use the console.

enter image description here


In case if website hosting is enabled for the bucket, there is an alternative way to add 301 redirect. According to it redirection rules are described on the bucket level in XML-format, and can be specified in bucket's properties via AWS S3 Console (Static Website Hosting section). Full documentation about its syntax currently can be found here.

This approach is convenient when you have massive URL movements as far as it's easier to manage all redirections in one place. For instance it's possible to define redirection rules

<RoutingRule>
    <Condition>
        <KeyPrefixEquals>index.php</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
</RoutingRule>
<RoutingRule>
    <Condition>
        <KeyPrefixEquals>blog.php?id=21</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <ReplaceKeyWith>mysql-utf-8-database-creation-snippet.html</ReplaceKeyWith>
    </Redirect>
</RoutingRule>

It looks like it's the same as creating fake objects and specifying x-amz-website-redirect-location metadata for them. The bad news are that there are could be not more than 50 such rules in XML for one bucket. And yes, it's not convenient to manage XML. But for me this way is easier at present. Again, because it's easier to manage all files in one place.

This XML-approach is extremely useful when you, say, renamed a directory with a lot of pages. In this case it's necessary to create a single redirection rule for directory, instead of separate rule for each page inside it. For instance

<RoutingRule>
    <Condition>
        <KeyPrefixEquals>blog/</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <ReplaceKeyPrefixWith>it-blog/</ReplaceKeyPrefixWith>
    </Redirect>
</RoutingRule>

According to this rule example.com/blog/whatever will be redirected to example.com/it-blog/whatever.

Another useful feature of such approach is that it replaces only prefixes. In the same way as with directories it's possible to redirect page, but save query parameters. It could be suitable if there is some JS processing of these query parameters. With x-amz-website-redirect-location metadata you'll probably loose them.

As I mentioned writing and reading XML might be inconvenient. To overcome this I wrote a simple online tool in GWT to convert plain text with former and new URLs to XML format. It uses the KeyPrefixEquals predicate and execute ReplaceKeyPrefixWith redirect.

Finally, according to documentation, if website hosting is disabled, redirection support is not applicable for this bucket.


The ability has been added recently: http://aws.typepad.com/aws/2012/10/amazon-s3-support-for-website-redirects.html