• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / Amazon S3 URL Redirect

Amazon S3 URL Redirect

Last updated on July 21, 2015 by Sal Ferrarello

Recently I found it necessary to create an Amazon S3 URL redirect. In the case where the Amazon S3 bucket is setup to act as a website, you can setup this up by adding the redirect directly to the file metadata.

Instructions to setup an Amazon S3 website URL redirect

Unfortunately, in this case the S3 bucket was not setup to act as a website. While I could convert the bucket to a website, this would not correct the issue since the non-website S3 endpoint URLs were already in the wild and were the URLs that needed to be redirected.

Amazon S3 URL Redirect with a non-website endpoint URL

1. Create a new empty file redirect.html

2. Add HTML to this new file

Open the new file with you favorite text editor and add the following content to it

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Refresh" content="0; URL=http://www.example.com/target/">
<title>Redirect</title>
</head>
<body>
<a href="http://www.example.com/target/">http://www.example.com/target/</a>
</body>
</html>
view raw original-filename hosted with ❤ by GitHub

3. Set your Redirect Target

Modify your HTML in redirect.html, changing http://www.example.com/target/ to the target of your redirect (where you want your redirect to go).

This change needs to be made in three places.

4. Rename redirect.html

Rename redirect.html to the name of the file you are redirecting away from

e.g. if the original file is example.png, change the file to this name

Note: This change will include changing the extension (.html) to whatever extension matches the original file. I recognize this will feel odd having an HTML file with the wrong extension (like .png), just go with it

5. Upload this new file in S3

Upload your newly renamed file into the bucket (and directory) where the old file was located. We are overwriting the old file.

6. Modify the Metadata Content-Type

As a file step, we need to indicate the file is text/html even though the file extension may not indicate that. We do that by modifying the properties of the file in the Amazon AWS S3 Console and changing the Metadata Content-Type to text/html.

Metadata Content-Type: text/html

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Dev Tips Tagged With: AWS, S3

Reader Interactions

Comments

  1. Brad Spalding says

    June 26, 2018 at 2:49 pm

    Sal,

    Thanks for this article. It was a real lifesaver for us when an incorrect document was linked in a promotional email. Keep up the great work!

    Reply
    • Sal Ferrarello says

      June 26, 2018 at 3:08 pm

      That’s terrific (your situation sounds similar to mine own that lead to this article). Thanks for your comment, it made my day.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in