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> |
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
.
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!
That’s terrific (your situation sounds similar to mine own that lead to this article). Thanks for your comment, it made my day.
This is amazing. Such a simple, quick, and effective solution. I usually overwrite my files in AWS when they’re updated, but this was the one time it made sense to change the file name — and of course, a bunch of emails went out with the old URL. Thank you so much!