Configuring static website hosting
Overview
To configure a bucket for static website hosting, add the site configuration to the bucket. The configuration includes the following information:
Index document
When you enter a URL such as http://example.com, you are not requesting a specific page. In this case, the Web server will provide a default page that contains the directory that stores the requested website content. This default page is called an index document, and in most cases it is named as index.html. When you configure a bucket for website hosting, you must specify an index document. When a request is sent to the root domain or any subfolder, OBS returns this index document.
Error document
If an error occurs, OBS returns an HTML error document. For 4XX errors, you can selectively provide your own customized error document. You can provide other guides to your users in this document.
Redirection of all requests
If the root domain is example.com and you need to respond to requests from http://example.com and http://www.example.com, you can create two buckets named example.com and www.example.com. Then you can retain the website content in only one bucket (such as example.com), and configure the other bucket to redirect all requests to the example.com bucket.
Redirection based on advanced conditions
You can redirect requests based on the specific object name or prefix in the request, or based on the response code. For example, assume that you delete or rename an object in a bucket. You can add a routing rule that redirects requests to other objects.
The syntax format is as follows by setting redirection rules of specific requests:
<WebsiteConfiguration>
<IndexDocument>
<Suffix>index.html</Suffix></IndexDocument>
<ErrorDocument>
<Key>SomeErrorDocument.html</Key>
</ErrorDocument>
<RoutingRules>
............RoutingRules......
</RoutingRules>
</WebsiteConfiguration>
|
Parameter |
Description |
Mandatory |
|---|---|---|
|
WebsiteConfiguration |
Indicates the root element for the website configuration. Type: container Ancestor: none |
Yes |
|
RedirectAllRequestsTo |
Describes the redirection behavior for every request to this bucket's website endpoint. If this element is present, no other siblings are allowed. Type: container Ancestor: WebsiteConfiguration |
Yes |
|
HostName |
Indicates the name of the host where requests will be redirected. Type: string Ancestor: RedirectAllRequestsTo |
Yes |
|
Protocol |
Indicates the protocol (HTTP and HTTPS) used in redirecting requests. The default protocol is HTTP. Type: string Ancestor: RedirectAllRequestsTo |
No |
The syntax format of RoutingRules is as follows. The content in square brackets is optional.
<RoutingRules> =
<RoutingRules>
<RoutingRule>...</RoutingRule>
[<RoutingRule>...</RoutingRule> ...]
</RoutingRules>
<RoutingRule> =
<RoutingRule>
[ <Condition>...</Condition> ]
<Redirect>...</Redirect>
</RoutingRule>
<Condition> =
<Condition>
[ <KeyPrefixEquals>...</KeyPrefixEquals> ]
[ <HttpErrorCodeReturnedEquals>...</HttpErrorCodeReturnedEquals> ]
</Condition>
<Redirect> =
<Redirect>
[ <HostName>...</HostName> ]
[ <Protocol>...</Protocol> ]
[ <ReplaceKeyPrefixWith>...</ReplaceKeyPrefixWith> ]
[ <ReplaceKeyWith>...</ReplaceKeyWith> ][
<HttpRedirectCode>...</HttpRedirectCode> ]
</Redirect>
|
Parameter |
Description |
Mandatory |
|---|---|---|
|
WebsiteConfiguration |
Indicates the root element for the website configuration. Type: container Ancestor: none |
Yes |
|
IndexDocument |
Indicates the container for the Suffix element. Type: container Ancestor: WebsiteConfiguration |
Yes |
|
Suffix |
Indicates a suffix that is appended to a request initiated for a directory on the website endpoint. For example, if the suffix is index.html and you request for samplebucket/images/, the data that is returned will be for the object with the key name images/index.html in the samplebucket bucket. The suffix cannot be empty or contain slashes (/). Type: string Ancestor: IndexDocument |
Yes |
|
ErrorDocument |
Indicates the container for the Key element. Type: container Ancestor: WebsiteConfiguration |
No |
|
Key |
Indicates the object name that is used when a 4XX error occurs. This element identifies the page that is returned when a 4XX error occurs. Type: string Ancestor: ErrorDocument Condition: Required when ErrorDocument is specified. |
No |
|
RoutingRules |
Indicates the container for the RoutingRule element. Type: container Ancestor: WebsiteConfiguration |
No |
|
RoutingRule |
Indicates the container for a routing rule. A redirection rule contains a Condition and a Redirect. When the Condition is matched, Redirect takes effect. Type: container Ancestor: RoutingRules Condition: In a RoutingRules container, there must be at least one RoutingRule element. |
Yes |
|
Condition |
Indicates the container for describing a condition that must be met for the specified redirect to apply. Type: container Ancestor: RoutingRule |
No |
|
KeyPrefixEquals |
Indicates the object name prefix when the redirection is applied. For example: To redirect the request for object ExamplePage.html, the key prefix is set to ExamplePage.html. Type: string Ancestor: Condition Condition: Required when the ancestor element Condition is specified and sibling HttpErrorCodeReturnedEquals is not specified. If two conditions are specified, both conditions must be true for the Redirect to be applied. |
No |
|
HttpErrorCodeReturnedEquals |
Indicates the HTTP error code returned when the Redirect is applied. The specified redirect is applied only when the error code returned equals this value. For example: If you want to redirect to NotFound.html when HTTP error code 404 is returned, set HttpErrorCodeReturnedEquals to 404 in Condition and ReplaceKeyWith to NotFound.html in Redirect. Type: string Ancestor: Condition Condition: Required when ancestor element Condition is specified and sibling KeyPrefixEquals is not specified. If multiple conditions are specified, the Redirect takes effect only after all conditions are met. |
No |
|
Redirect |
Indicates the container for redirection information. You can redirect requests to another host, to another web page, or with another protocol. You can specify an error code to be returned after an error. Type: container Ancestor: RoutingRule |
Yes |
|
Protocol |
Indicates the protocol used in the redirection request. Type: string Ancestor: Redirect Valid values: http, https Condition: Not required if one of the siblings is present. |
No |
|
HostName |
Indicates the host name used in the redirection request. Type: string Ancestor: Redirect Condition: Not required if one of the siblings is present. |
No |
|
ReplaceKeyPrefixWith |
Indicates the object name prefix used in the redirection request. For example: To redirect all requests for (objects under) docs to (objects under) documents, set KeyPrefixEquals to docs in Condition and ReplaceKeyPrefixWith to documents in Redirect. Type: string Ancestor: Redirect Condition: Not required if one of the siblings is present. Can be present only if ReplaceKeyWith is not provided. |
No |
|
ReplaceKeyWith |
Indicates the object name used in the redirection request. For example, redirect requests to error.html. Type: string Ancestor: Redirect Condition: Not required if one of the siblings is present. Can be present only if ReplaceKeyPrefixWith is not provided. |
No |
|
HttpRedirectCode |
Indicates the HTTP status code returned after the redirection request. Type: string Ancestor: Redirect Condition: Not required if one of the siblings is present. |
No |
Examples:
Example 1: Modifying the object name prefix for redirection.
Assume that your bucket contains the following objects:
index.html
docs/article1.html
docs/article2.html
You decide to change the folder name from docs/ to documents/. After the modification, the request for an object with prefix /docs needs to be redirected to another with documents/. For example, the request for docs/article1.html needs to be redirected to documents/article1.html.
In this case, you can add the following routing rules to the website configuration:
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals>docs/</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith>
</Redirect>
</RoutingRule>
</RoutingRules>
Example 2: Redirecting requests destined for deleted folders to a specified page
Assume that you have deleted the images/ folder, that is, you have deleted all objects whose object name prefix is images/. You can add a routing rule that redirects the requests of all objects whose prefix is images/ to the page named folderdeleted.html.
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals>images/</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyWith>folderdeleted.html</ReplaceKeyWith>
</Redirect>
</RoutingRule>
</RoutingRules>
Example 3: Redirecting for HTTP errors
Assume that the requested object is not found, and the request needs to be redirected to www.example.com. You can add redirection rules so that site visitors can redirect to www.example.com when HTTP status code 404 (not found) is returned. The following example also inserts the object name prefix report-404/ into redirection. For example, if you request the page ExamplePage.html and it causes an HTTP 404 error, the request will be redirected to the page report-404/ExamplePage.html on the www.example.com. If there is no routing rule and HTTP error 404 occurs, the error document specified in the configuration is returned.
<RoutingRules>
<RoutingRule>
<Condition>
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals >
</Condition>
<Redirect>
<HostName>www.example.com</HostName>
<ReplaceKeyPrefixWith>report-404/</ReplaceKeyPrefixWith>
</Redirect>
</RoutingRule>
</RoutingRules>
Index document support
An index document is a web page returned when a request is sent to the root of a site or any subfolder. For example, if the user enters http://www.example.com in the browser, the user does not request any specific page. In this case, OBS provides index documents, which are sometimes referred to as default pages.
When configuring your bucket as a website, provide the name of the index document. You must upload the object with this name and configure it to be public and accessible.
Trailing slash in the root URL is optional. For example, if you configure an index.html website as an index document, any of the following URLs will return to index.html.
http://bucketname.obs-website.example.com http://bucketname.obs-website.example.com
In OBS, objects are horizontally stored in buckets. It does not provide any hierarchical organization as a file system on a computer. You can create a logical hierarchy by using the object name representing the folder structure. For example, consider a bucket with three objects and the following object names.
- sample1.jpg
- photos/2006/Jan/sample2.jpg
- photos/2006/Feb/sample3.jpg
Although they are not stored as any physical hierarchical organization, you can infer the following logical folder structure from the object name.
- The sample1.jpg object is located at the root level of the bucket.
- The sample2.jpg object is located in the photos/2006/Jan subfolder.
- The sample3.jpg object is located in the photos/2006/Feb subfolder.
Customized error document support
The following table lists the subset of the HTTP response code returned by OBS when an error occurs.
|
HTTP error code |
Description |
|---|---|
|
301 Moved Permanently |
When a user sends a request to an endpoint of OBS, a 301 Moved Permanently response is returned. |
|
302 Found |
When OBS receives a request for key x that does not end with a slash, it searches for the object whose key name is x. If the object is not found, OBS determines that the request is sent for subfolder x. Then OBS redirects the request by adding a slash at the end of the request, and returns 302 Found. |
|
304 Not Modified |
OBS users request the If-Modified-Since and If-Unmodified-Since headers to determine whether the requested object is the same as the cached copy stored on the client. If the objects are the same, the website terminal node returns a 304 Not Modified response. |
|
403 Forbidden |
When the user request is converted into an object that cannot be publicly read, the response of the website terminal node includes 403 Forbidden. The object owner must use bucket policies or ACLs to make the object public and accessible. |
|
404 Not Found |
The response of the website terminal node contains 404 Not Found due to the following reasons:
You can create customized documents returned for 404 Not Found. Ensure that the document has been uploaded to the bucket configured as a website and the website hosting configuration has been set to use the document. |
|
500 Service Error |
When an internal server error occurs, the response of the website terminal node contains 500 Service Error. |
|
503 Service Unavailable |
When the OBS determines that you need to reduce the request frequency, the response of the website terminal node contains 503 Service Unavailable. |
Permissions required for website access
When you configure a bucket as a website, you must make the object to be provided public and accessible. To perform this operation, you need to write a bucket policy that grants GetObject permissions to everyone. If the object requested by the user does not exist on the terminal node of the website, OBS returns the HTTP response code 404 Not Found. If the object exists but you do not grant read permission for the object, the website terminal node returns HTTP response code 403 Access Denied. Users can use this response code to infer whether a particular object exists. If you do not need this function, do not enable the bucket website support.
The following example bucket policy grants each person the permission to access objects in a specified folder. For details about bucket policies, see Bucket Policy.
{
"Statement":[{
"Sid":"PublicReadGetObject",
"Effect":"Allow",
"Principal": {"ID":"*"},
"Action":["GetObject"],
"Resource":["example-bucket/*" ]
}
]
}
A bucket policy applies only to objects owned by a bucket owner. If the bucket contains objects that are not owned by the bucket owner, use object ACLs to grant the public READ permission to the objects.
You can use bucket policies or object ACLs to grant public read permissions to your objects. To use ACLs to make objects public and accessible, you can grant READ permission to everyone, as shown in the following authorization elements. You can add the authorization element to object ACLs. For details about managing ACLs, see ACL.
<Grant>
<Grantee>
<Canned>Everyone</Canned>
</Grantee>
<Permission>READ</Permission>
</Grant>
Last Article: Principles of static website hosting
Next Article: Configuring Flash for Cross-domain Access
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.