(Optional) Configuring a webpage redirect
If your Amazon S3 bucket is configured for static website hosting, you can configure redirects for your bucket or the objects in it. You have the following options for configuring redirects.
Topics
Redirect requests for your bucket's website endpoint to another bucket or domain
You can redirect all requests to a website endpoint for a bucket to another bucket or domain. If you redirect all requests, any request made to the website endpoint is redirected to the specified bucket or domain.
For example, if your root domain is example.com
, and you want to serve requests for both http://example.com
and http://www.example.com
, you must create two buckets named
example.com
and www.example.com
. Then, maintain the content in the example.com
bucket, and configure the other www.example.com
bucket to redirect all requests to the
example.com
bucket. For more information, see Configuring a Static Website Using a Custom Domain Name.
To redirect requests for a bucket website endpoint
-
Open the Amazon S3 console at https://console.aws.amazon.com/s3/
. -
Under Buckets, choose the name of the bucket that you want to redirect requests from (for example,
www.example.com
). -
Choose Properties.
-
Under Static website hosting, choose Edit.
-
Choose Redirect requests for an object.
-
In the Host name box, enter the website endpoint for your bucket or your custom domain.
For example, if you are redirecting to a root domain address, you would enter
example.com
. -
For Protocol, choose the protocol for the redirected requests (none,http, or https).
If you do not specify a protocol, the default option is none.
-
Choose Save changes.
Configure redirection rules to use advanced conditional redirects
Using advanced redirection rules, you can route requests conditionally according to specific object key names, prefixes in the request, or response codes. For example, suppose that you delete or rename an object in your bucket. You can add a routing rule that redirects the request to another object. If you want to make a folder unavailable, you can add a routing rule to redirect the request to another webpage. You can also add a routing rule to handle error conditions by routing requests that return the error to another domain when the error is processed.
When enabling static website hosting for your bucket, you can optionally specify advanced redirection rules. Amazon S3 has a limitation of 50 routing rules per website configuration. If you require more than 50 routing rules, you can use object redirect. For more information, see Using the S3 console.
For more information about configuring routing rules using the REST API, see PutBucketWebsite in the Amazon Simple Storage Service API Reference.
Important
To create redirection rules in the new Amazon S3 console, you must use JSON. For JSON examples, see Redirection rules examples.
To configure redirection rules for a static website
To add redirection rules for a bucket that already has static website hosting enabled, follow these steps.
-
Open the Amazon S3 console at https://console.aws.amazon.com/s3/
. -
In the Buckets list, choose the name of a bucket that you have configured as a static website.
-
Choose Properties.
-
Under Static website hosting, choose Edit.
-
In Redirection rules box, enter your redirection rules in JSON.
In the S3 console you describe the rules using JSON. For JSON examples, see Redirection rules examples. Amazon S3 has a limitation of 50 routing rules per website configuration.
-
Choose Save changes.
Routing rule elements
The following is general syntax for defining the routing rules in a website configuration in JSON and XML To configure redirection rules in the new S3 console, you must use JSON. For JSON examples, see Redirection rules examples.
The following table describes the elements in the routing rule.
Name | Description |
---|---|
RoutingRules |
Container for a collection of RoutingRule
elements. |
RoutingRule |
A rule that identifies a condition and the redirect that is applied when the condition is met. Condition:
|
Condition |
Container for describing a condition that must be met for the specified redirect to be applied. If the routing rule does not include a condition, the rule is applied to all requests. |
KeyPrefixEquals |
The prefix of the object key name from which requests are redirected.
|
HttpErrorCodeReturnedEquals |
The HTTP error code that must match for the redirect to apply. If an error occurs, and if the error code meets this value, then the specified redirect applies.
|
Redirect |
Container element that provides instructions for
redirecting the request. You can redirect requests to
another host or another page, or you can specify another
protocol to use. A |
Protocol |
The protocol, If one of its siblings is supplied, |
HostName |
The hostname to be used in the If one of its siblings is supplied, |
ReplaceKeyPrefixWith |
The prefix of the object key name that replaces the value
of If one of its siblings is supplied,
|
ReplaceKeyWith |
The object key to be used in the If one of its siblings is supplied,
|
HttpRedirectCode |
The HTTP redirect code to be used in the
If one of its siblings is supplied,
|
Redirection rules examples
The following examples explain common redirection tasks:
Important
To create redirection rules in the new Amazon S3 console, you must use JSON.
Example 1: Redirect after renaming a key prefix
Suppose that your bucket contains the following objects:
-
index.html
-
docs/article1.html
-
docs/article2.html
You decide to rename the folder from docs/
to
documents/
. After you make this change, you need to
redirect requests for prefix docs/
to
documents/
. For example, request for
docs/article1.html
will be redirected to
documents/article1.html
.
In this case, you add the following routing rule to the website configuration.
Example 2: Redirect requests for a deleted folder to a page
Suppose that you delete the images/
folder (that is, you
delete all objects with the key prefix images/
). You can
add a routing rule that redirects requests for any object with the key
prefix images/
to a page named
folderdeleted.html
.
Example 3: Redirect for an HTTP error
Suppose that when a requested object is not found, you want to
redirect requests to an Amazon Elastic Compute Cloud (Amazon EC2) instance. Add a redirection
rule so that when an HTTP status code 404 (Not
Found)
is returned, the site visitor is redirected to an
Amazon EC2 instance that handles the request.
The following example also inserts the object key prefix
report-404/
in the redirect. For example, if you
request a page ExamplePage.html
and it results in an HTTP
404 error, the request is redirected to a page
report-404/ExamplePage.html
on the specified Amazon EC2
instance. If there is no routing rule and the HTTP error 404 occurs, the
error document that is specified in the configuration is
returned.
Redirect requests for an object
You can redirect requests for an object to another object or URL by setting the
website redirect location in the metadata of the object. You set the redirect by
adding the x-amz-website-redirect-location
property to the object
metadata. On the Amazon S3 console, you set the Website Redirect
Location in the metadata of the object. If you use the Amazon S3 API, you set
x-amz-website-redirect-location
. The website then interprets the
object as a 301 redirect.
To redirect a request to another object, you set the redirect location to the key of the target object. To redirect a request to an external URL, you set the redirect location to the URL that you want. For more information about object metadata, see System-defined object metadata.
When you set a page redirect, you can either keep or delete the source object
content. For example, if you have a page1.html
object in your bucket,
you can redirect any requests for this page to another object,
page2.html
. You have two options:
-
Keep the content of the
page1.html
object and redirect page requests. -
Delete the content of
page1.html
and upload a zero-byte object namedpage1.html
to replace the existing object and redirect page requests.
-
Open the Amazon S3 console at https://console.aws.amazon.com/s3/
. -
In the Buckets list, choose the name of the bucket that you have configured as a static website (for example,
example.com
). -
Under Objects, select your object.
-
Choose Actions, and choose Edit metadata.
-
Choose Metadata.
-
Choose Add Metadata.
-
Under Type, choose System Defined.
-
In Key, choose x-amz-website-redirect-location.
-
In Value, enter the key name of the object that you want to redirect to, for example,
/page2.html
.For another object in the same bucket, the
/
prefix in the value is required. You can also set the value to an external URL, for example,http://www.example.com
. -
Choose Edit metadata.
The following Amazon S3 API actions support the
x-amz-website-redirect-location
header in the request. Amazon S3
stores the header value in the object metadata as
x-amz-website-redirect-location
.
A bucket configured for website hosting has both the website endpoint and the REST endpoint. A request for a page that is configured as a 301 redirect has the following possible outcomes, depending on the endpoint of the request:
-
Region-specific website endpoint – Amazon S3 redirects the page request according to the value of the
x-amz-website-redirect-location
property. -
REST endpoint – Amazon S3 doesn't redirect the page request. It returns the requested object.
For more information about the endpoints, see Key differences between a website endpoint and a REST API endpoint.
When setting a page redirect, you can either keep or delete the object
content. For example, suppose that you have a page1.html
object
in your bucket.
-
To keep the content of
page1.html
and only redirect page requests, you can submit a PUT Object - Copy request to create a newpage1.html
object that uses the existingpage1.html
object as the source. In your request, you set thex-amz-website-redirect-location
header. When the request is complete, you have the original page with its content unchanged, but Amazon S3 redirects any requests for the page to the redirect location that you specify. -
To delete the content of the
page1.html
object and redirect requests for the page, you can send a PUT Object request to upload a zero-byte object that has the same object key:page1.html
. In the PUT request, you setx-amz-website-redirect-location
forpage1.html
to the new object. When the request is complete,page1.html
has no content, and requests are redirected to the location that is specified byx-amz-website-redirect-location
.
When you retrieve the object using the GET Object action, along
with other object metadata, Amazon S3 returns the
x-amz-website-redirect-location
header in the
response.