Configure a Bucket for Website Hosting
To configure a bucket for static website hosting, you add a website configuration to your bucket. The configuration includes the following information:
When you type a URL such as http://example.com you are not requesting a specific page. In this case the web server serves a default page, for the directory where the requested website content is stored. This default page is referred to as index document, and is typically named
index.html. When you configure a bucket for website hosting, you must specify an index document. Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders. For more information, see Index Documents and Folders .
If an error occurs, Amazon S3 returns an HTML error document. For 4XX class errors, you can optionally provide your own custom error document, in which you can provide additional guidance to your users. For more information, see Custom Error Document Support .
Redirects all requests
If your root domain is
example.comand you want to serve requests for both
http://www.example.com, you can create two buckets named
www.example.com, maintain website content in only one bucket, say,
example.com, and configure the other bucket to redirect all requests to the
Advanced conditional redirects
You can conditionally route requests according to specific object key names or prefixes in the request, or according to the response code. 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. Suppose that you want to make a folder unavailable. You can add a routing rule to redirect the request to another page, which explains why the folder is no longer available. You can also add a routing rule to handle an error condition by routing requests that return the error to another domain, where the error will be processed.
You can manage your buckets website configuration using the Amazon S3 console. The bucket Properties panel in the console enables you to specify the website configuration.
To host a static website on Amazon S3, you need only provide the name of the index document.
To redirect all requests to the bucket's website endpoint to another host, you only need to provide host name.
However, when configuring bucket for website hosting, you can optionally specify advanced redirection rules.
You describe the rules using XML. The following section provides general syntax and examples of specifying redirection rules.
Syntax for Specifying Routing Rules
The following is a general syntax for defining the routing rules in a website configuration:
<RoutingRules> = <RoutingRules> <RoutingRule>...</RoutingRule> [<RoutingRule>...</RoutingRule> ...] </RoutingRules> <RoutingRule> = <RoutingRule> [ <Condition>...</Condition> ] <Redirect>...</Redirect> </RoutingRule> <Condition> = <Condition> [ <KeyPrefixEquals>...</KeyPrefixEquals> ] [ <HttpErrorCodeReturnedEquals>...</HttpErrorCodeReturnedEquals> ] </Condition> Note: <Condition> must have at least one child element. <Redirect> = <Redirect> [ <HostName>...</HostName> ] [ <Protocol>...</Protocol> ] [ <ReplaceKeyPrefixWith>...</ReplaceKeyPrefixWith> ] [ <ReplaceKeyWith>...</ReplaceKeyWith> ] [ <HttpRedirectCode>...</HttpRedirectCode> ] </Redirect> Note: <Redirect> must have at least one child element. Also, you can have either ReplaceKeyPrefix with or ReplaceKeyWith, but not both.
The following table describes the elements in the routing rule.
Container for a collection of RoutingRule elements.
A rule that identifies a condition and the redirect that is applied when the condition is met.
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.
The object key name prefix from which requests will be redirected.
The HTTP error code that must match for the redirect to apply. In the event of an error, if the error code meets this value, then specified redirect applies.
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 RoutingRule must have a
The protocol, http or https, to be used in the Location header that is returned in the response.
The host name to be used in the Location header that is returned in the response.
The object key name prefix that will replace the value of
The object key to be used in the Location header that is returned in the response.
The HTTP redirect code to be used in the Location header that is returned in the response.
The following are some of the examples:
Example 1: Redirect after renaming a key prefix
Suppose your bucket contained the following objects:
Now you decided to rename the folder from
documents/. After you make this change, you will need to
redirect requests for prefix
example, request for
docs/article1.html will need to be redirected
In this case you add the following routing rule to the website configuration:
<RoutingRules> <RoutingRule> <Condition> <KeyPrefixEquals>docs/</KeyPrefixEquals> </Condition> <Redirect> <ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith> </Redirect> </RoutingRule> </RoutingRules>
Example 2: Redirect requests for a deleted folder to a page
Suppose you delete the
images/ folder (that is, you delete all
objects with 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.
<RoutingRules> <RoutingRule> <Condition> <KeyPrefixEquals>images/</KeyPrefixEquals> </Condition> <Redirect> <ReplaceKeyWith>folderdeleted.html</ReplaceKeyWith> </Redirect> </RoutingRule> </RoutingRules>
Example 3: Redirect for an HTTP error
Suppose that when a requested object is not found, you want to redirect
requests to an Amazon EC2 instance. You can add a redirection rule so that when an
HTTP status code 404 (Not Found) is returned the site visitor is redirected to
an EC2 instance that will handle 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 a HTTP 404 error, the
request is redirected to a page
report-404/ExamplePage.html on the
specified EC2 instance. If there is no routing rule and the HTTP error 404
occurs, the error document specified in the configuration is returned.
<RoutingRules> <RoutingRule> <Condition> <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals > </Condition> <Redirect> <HostName>ec2-11-22-333-44.compute-1.amazonaws.com</HostName> <ReplaceKeyPrefixWith>report-404/</ReplaceKeyPrefixWith> </Redirect> </RoutingRule> </RoutingRules>