Tuesday, November 13, 2018

How to redirect in Amazon S3 website hosting

If you happen to use Amazon S3 for static website hosting and also need some redirect, good news is that they provide redirect ability in the service. This means that we don't need to set up another Apache for redirect.



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 has at least one child element.

<Redirect> =
   <Redirect> 
      [ <HostName>...</HostName> ]
      [ <Protocol>...</Protocol> ]
      [ <ReplaceKeyPrefixWith>...</ReplaceKeyPrefixWith>  ]
      [ <ReplaceKeyWith>...</ReplaceKeyWith> ]
      [ <HttpRedirectCode>...</HttpRedirectCode> ]
   </Redirect>
    Note: <Redirect> must has 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.
NameDescription
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: A RoutingRules, container must contain at least one routing rule.
ConditionContainer 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 object key name prefix from which requests will be redirected.
KeyPrefixEquals is required if HttpErrorCodeReturnedEquals is not specified. If both KeyPrefixEquals andHttpErrorCodeReturnedEquals are specified, both must be true for the condition to be met.
HttpErrorCodeReturnedEquals
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.
HttpErrorCodeReturnedEquals is required if KeyPrefixEquals is not specified. If both KeyPrefixEquals andHttpErrorCodeReturnedEquals are specified, both must be true for the condition to be met.
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 RoutingRule must have a Redirect element. A Redirectelement must contain at least one of the following elements: ProtocolHostNameReplaceKeyPrefixWith,ReplaceKeywith or HttpRedirectCode.
Protocol
The protocol, http or https, to be used in the Location header that is returned in the response.
Protocol is not required if one of its siblings is supplied.
HostName
The host name to be used in the Location header that is returned in the response.
HostName is not required if one of it's siblings is supplied.
ReplaceKeyPrefixWith
The object key name prefix that will replace the value of KeyPrefixEquals in the redirect request.
ReplaceKeyPrefixWith is not required if one of it's siblings is supplied. It can be supplied only if ReplaceKeyWithis not supplied.
ReplaceKeyWith
The object key to be used in the Location header that is returned in the response.
ReplaceKeyWith is not required if one of it's siblings is supplied. It can be supplied only if ReplaceKeyPrefixWithis not supplied.
HttpRedirectCode
The HTTP redirect code to be used in the Location header that is returned in the response.
HttpRedirectCode is not required if one of it's siblings is supplied.
The following are some of the examples:

Example 1: Redirect after renaming a key prefix
Suppose your bucket contained the following objects:

index.html
docs/article1.html
docs/article2.html
Now you decided to rename the folder from docs/ to documents/. After you make this change, you will need to redirect requests for prefix /docs to documents/. For example, request for docs/article1.html will need to be redirected to documents/article1.html.

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>


Suppose we want to redirect it.i88.ca/images/ to blog.i88.ca:

<RoutingRules>
    <RoutingRule>
        <Condition>
            <KeyPrefixEquals>images/</KeyPrefixEquals>
        </Condition>
        <Redirect>
            <HostName>blog.i88.ca</HostName>
        </Redirect>
    </RoutingRule>
</RoutingRules>