Copied.
Redirecting Requests
Redirection Overview
When using static website hosting, you can configure redirection to redirect specific or all requests.
If the structure, address, or file name extension of a website is changed, users will fail to access the website using the old address (such as the address saved in the folder of favorites), and the 404 error message is returned. In this case, you can configure redirection for the website to redirect user access requests to a specified page rather than a 404 error page.
Typical use cases for request redirection include:
- Redirecting all requests to another website
- Defining specific redirection rules to redirect selected requests
This section describes three redirection scenarios:
- Redirecting all requests to another bucket or URL: When configuring static website hosting for a bucket, set Hosting Type to Redirect requests.
- Redirecting requests for objects with a specific prefix or for a specific object to another object or URL: When configuring static website hosting for a bucket, set Hosting Type to Host a static website and configure the redirection rule.
- Redirecting requests for the default homepage of a static website to another object in the same bucket or to a URL:
- When configuring static website hosting for a bucket, set Hosting Type to Host a static website and specify the name of the static webpage file in the bucket's root directory as the default homepage.
- Add the WebsiteRedirectLocation metadata to the webpage file in the root directory.
Prerequisites
Web page files required for static website hosting have been uploaded to the specified bucket.
The static website files hosted in the bucket are accessible to all users.
Static web page files in the Archive or Deep Archive storage class have been restored. For more information, see Restoring Archive or Deep Archive Objects.
Redirecting All Requests to Another Bucket or URL
To redirect all requests to another bucket or URL, set Hosting Type to Redirect requests when configuring static website hosting for the bucket.
You can configure this setting using OBS Console, APIs, or SDKs, but not using OBS Browser+ or obsutil.
Using OBS Console
- In the navigation pane of OBS Console, choose Buckets.
- In the bucket list, click the desired bucket. The Objects page is displayed.
- In the navigation pane, choose Data Management > Static Website Hosting.
- Click Configure Static Website Hosting. The Configure Static Website Hosting dialog box is displayed.
- Enable Status.
- Set Hosting Type to Redirect requests, as shown in Figure 1. In the text box of Redirect To, enter the bucket's access domain name or URL.
- Enter the access domain name of another bucket to redirect all requests to the current bucket to that bucket. The Access Domain Name displayed in the Domain Name Details area on the bucket overview page is the bucket's access domain name. For details, see Viewing Bucket Information.
- Enter a URL to redirect all requests to the current bucket to another webpage. For example, to redirect requests to a website's root domain, example.com, enter example.com.
- Click OK.
- (Optional) If you are redirecting requests to another bucket, configure a bucket policy to allow all users to access the static website files in the bucket. If you have already configured the policy, skip this step.
- In the bucket list, find the bucket to redirect requests to and click its name to open the object list page.
- In the navigation pane, choose Permissions > Bucket Policies.
- Click Create.
- Configure the bucket policy.
This step describes only the required key parameters. For other parameters, retain the default settings. For more information, see Granting All Accounts the Read Permission for Certain Objects.
Figure 2 Configuring the read-only permission for objects
Table 1 Bucket policy parameters Parameter
Example
Description
Configuration mode
Visual Editor
Select a policy configuration mode: Visual Editor or JSON.
- Visual Editor: You can configure through a visual interface in an easy way.
- JSON: It lets you configure the policy in code using JSON syntax. For details about the JSON view, see Creating a Custom Bucket Policy (JSON View).
Policy Name
policy-bucket
Enter a custom policy name that is easy to remember.
Effect
Allow
Specifies whether to allow or deny authorized users to perform specific actions on authorized resources under defined conditions.
Principal
All accounts
Select the users who can access the bucket.
You can specify one or more regular accounts or delegated accounts. You must specify regular accounts, delegated accounts, or both.
- All accounts: Grants bucket access to anyone without authentication. This may pose a security risk to your data.
- Current account: Grants bucket access to one or more IAM users in the current account.
- Other accounts: Grants bucket access to other accounts or their IAM users.
- Delegated accounts: Grants bucket access to a delegated account.
Enter the delegating account ID (ID of the account that creates the agency) and the agency name.
For example, if account A creates an agency Agency-A-to-B to authorize account B to operate resources in account A, enter account A's ID as the delegating account ID and Agency-A-to-B as the agency name.
To query the account ID, IAM user ID, delegating account ID, and agency name, see Obtaining Account, IAM User, Project, User Group, Region, and Agency Information.
Resources
Specified objects
Set the object name to *.
Select the resources to which the bucket policy applies.
- Entire bucket (including the objects in it): The bucket policy applies to the bucket and all objects in it.
- Current bucket: The bucket policy applies only to the bucket, excluding objects in it.
- Specified objects: The bucket policy applies to specified objects in the current bucket. Enter * as the object name to apply the bucket policy to all objects in the bucket.
- To match a specific object in the bucket, enter its full name, such as abc.txt.
- To match objects by prefix or suffix, use a wildcard (*). For example, enter a* to match objects whose names start with a or *c to match objects whose names end with c.
- To match objects in a folder, specify the folder name followed by a slash and the matching pattern. For example, testdir/* matches all objects in the testdir folder, testdir/a* matches objects in that folder whose names start with a, and testdir/*c matches objects in that folder whose names end with c.
Actions
Use a template > Object Read-Only
Select the actions that the bucket policy applies to for objects in the bucket.
- Use a template: Use a built-in OBS template to configure actions.
- Object Read-Only: Supports the following actions:
- GetObject: Obtains object content and metadata.
- GetObjectVersion: Obtains the content and metadata of a specific object version.
- GetObjectVersionAcl: Obtains the ACL of a specific object version.
- GetObjectAcl: Obtains the ACL of an object.
- RestoreObject: Restores Archive objects.
- Object Read/Write: Supports the following actions:
- PutObject: Uploads objects using PUT or POST, appends data to objects, initiates multipart uploads, uploads parts, completes multipart uploads, modifies objects, truncates objects, and renames objects.
- GetObject: Obtains object content and metadata.
- GetObjectVersion: Obtains the content and metadata of a specific object version.
- ModifyObjectMetaData: Modifies object metadata.
- ListMultipartUploadParts: Lists uploaded parts.
- AbortMultipartUpload: Aborts a multipart upload.
- GetObjectVersionAcl: Obtains the ACL of a specific object version.
- GetObjectAcl: Obtains the ACL of an object.
- PutObjectAcl: Sets the ACL of an object.
- RestoreObject: Restores Archive objects.
- Object Read-Only: Supports the following actions:
- Customize: Select the actions that the bucket policy applies to for objects in the bucket. For details, see Object Actions.
- Click Create.
The newly created bucket policy appears in the bucket policy list, indicating successful creation.
- Verification: Enter the current bucket's access domain name in a web browser. The browser is redirected to the bucket or URL specified in the redirection settings.
Due to browser caching, you may need to clear your browser cache to see the expected results.
Redirecting Requests for Objects with a Specific Prefix or for a Specific Object to Another Object or URL
To redirect requests for objects with a specific prefix or for a specific object to another object or URL, set Hosting Type to Host a static website and configure a redirection rule when configuring static website hosting for a bucket.
You can configure this setting using OBS Console, APIs, or SDKs, but not using OBS Browser+ or obsutil.
Using OBS Console
- (Optional) If the static website files in the bucket are not publicly accessible, configure a bucket policy to grant all users access to the files. If you have already configured the policy, skip this step.
- In the bucket list, find the bucket to redirect requests to and click its name to open the object list page.
- In the navigation pane, choose Permissions > Bucket Policies.
- Click Create.
- Configure the bucket policy.
This step describes only the required key parameters. For other parameters, retain the default settings. For more information, see Granting All Accounts the Read Permission for Certain Objects.
Figure 3 Configuring the read-only permission for objects
Table 2 Bucket policy parameters Parameter
Example
Description
Configuration mode
Visual Editor
Select a policy configuration mode: Visual Editor or JSON.
- Visual Editor: You can configure through a visual interface in an easy way.
- JSON: It lets you configure the policy in code using JSON syntax. For details about the JSON view, see Creating a Custom Bucket Policy (JSON View).
Policy Name
policy-bucket
Enter a custom policy name that is easy to remember.
Effect
Allow
Specifies whether to allow or deny authorized users to perform specific actions on authorized resources under defined conditions.
Principal
All accounts
Select the users who can access the bucket.
You can specify one or more regular accounts or delegated accounts. You must specify regular accounts, delegated accounts, or both.
- All accounts: Grants bucket access to anyone without authentication. This may pose a security risk to your data.
- Current account: Grants bucket access to one or more IAM users in the current account.
- Other accounts: Grants bucket access to other accounts or their IAM users.
- Delegated accounts: Grants bucket access to a delegated account.
Enter the delegating account ID (ID of the account that creates the agency) and the agency name.
For example, if account A creates an agency Agency-A-to-B to authorize account B to operate resources in account A, enter account A's ID as the delegating account ID and Agency-A-to-B as the agency name.
To query the account ID, IAM user ID, delegating account ID, and agency name, see Obtaining Account, IAM User, Project, User Group, Region, and Agency Information.
Resources
Specified objects
Set the object name to *.
Select the resources to which the bucket policy applies.
- Entire bucket (including the objects in it): The bucket policy applies to the bucket and all objects in it.
- Current bucket: The bucket policy applies only to the bucket, excluding objects in it.
- Specified objects: The bucket policy applies to specified objects in the current bucket. Enter * as the object name to apply the bucket policy to all objects in the bucket.
- To match a specific object in the bucket, enter its full name, such as abc.txt.
- To match objects by prefix or suffix, use a wildcard (*). For example, enter a* to match objects whose names start with a or *c to match objects whose names end with c.
- To match objects in a folder, specify the folder name followed by a slash and the matching pattern. For example, testdir/* matches all objects in the testdir folder, testdir/a* matches objects in that folder whose names start with a, and testdir/*c matches objects in that folder whose names end with c.
Actions
Use a template > Object Read-Only
Select the actions that the bucket policy applies to for objects in the bucket.
- Use a template: Use a built-in OBS template to configure actions.
- Object Read-Only: Supports the following actions:
- GetObject: Obtains object content and metadata.
- GetObjectVersion: Obtains the content and metadata of a specific object version.
- GetObjectVersionAcl: Obtains the ACL of a specific object version.
- GetObjectAcl: Obtains the ACL of an object.
- RestoreObject: Restores Archive objects.
- Object Read/Write: Supports the following actions:
- PutObject: Uploads objects using PUT or POST, appends data to objects, initiates multipart uploads, uploads parts, completes multipart uploads, modifies objects, truncates objects, and renames objects.
- GetObject: Obtains object content and metadata.
- GetObjectVersion: Obtains the content and metadata of a specific object version.
- ModifyObjectMetaData: Modifies object metadata.
- ListMultipartUploadParts: Lists uploaded parts.
- AbortMultipartUpload: Aborts a multipart upload.
- GetObjectVersionAcl: Obtains the ACL of a specific object version.
- GetObjectAcl: Obtains the ACL of an object.
- PutObjectAcl: Sets the ACL of an object.
- RestoreObject: Restores Archive objects.
- Object Read-Only: Supports the following actions:
- Customize: Select the actions that the bucket policy applies to for objects in the bucket. For details, see Object Actions.
- Click Create.
The newly created bucket policy appears in the bucket policy list, indicating successful creation.
- In the navigation pane, choose Data Management > Static Website Hosting.
- Click Configure Static Website Hosting. The Configure Static Website Hosting dialog box is displayed.
- Enable Status and configure parameters.
This step describes only the required key parameters. For other parameters, retain the default settings. To configure more parameters, see Configuring Static Website Hosting.
Figure 4 Configuring static website hosting
Table 3 Setting Hosting Type to Host a static website Parameter
Example
Description
Status
Enabled
Specifies whether to enable static website hosting.
Hosting Type
Host a static website
Select Host a static website.
Redirection Rule
See the following redirection rule examples.
Configure a redirection rule to redirect requests for objects with a specific prefix or for a specific object to another page or URL.
Redirection rules must be written in JSON format. You can define multiple rules, and each rule contains a Condition and a Redirect. Table 4 describes the parameters.
Table 4 Redirection parameters Container
Key
Description
Condition
KeyPrefixEquals
Optional. The object name prefix to which the redirection rule applies. If the requested object's name starts with the specified prefix, the redirection rule applies.
Example: To redirect all requests for the object ExamplePage.html object to another page or server, set KeyPrefixEquals to ExamplePage.html.
HttpErrorCodeReturnedEquals
Optional. The HTTP error code that triggers the redirection rule. If an error occurs and the error code matches the specified value, the redirection rule applies.
Example: To redirect requests that return HTTP status code 404 to NotFound.html, set HttpErrorCodeReturnedEquals to 404 in Condition and ReplaceKeyWith to NotFound.html in Redirect.
Redirect
Protocol
The protocol to use for redirected requests. It can be http or https. If this parameter is not specified, http is used by default.
HostName
The host name to which requests are redirected. If this parameter is not specified, requests are redirected to the host name in the original request.
ReplaceKeyPrefixWith
Redirects requests to objects whose names start with the specified prefix. Requests for objects matching KeyPrefixEquals are redirected to corresponding objects whose names start with ReplaceKeyPrefixWith.
You can use either ReplaceKeyPrefixWith or ReplaceKeyWith, but not both.
Example: To redirect all requests for objects in the docs directory to objects in the documents directory, set KeyPrefixEquals to docs in Condition and ReplaceKeyPrefixWith to documents in Redirect. Then, a request for the object docs/a.html is redirected to documents/a.html.
ReplaceKeyWith
Redirects requests to the specified object. Requests for objects matching KeyPrefixEquals are redirected to the object specified by ReplaceKeyWith.
You can use either ReplaceKeyWith or ReplaceKeyPrefixWith, but not both.
Example: To redirect all requests for objects in the docs directory to documents/error.html, set KeyPrefixEquals to docs in Condition and ReplaceKeyWith to documents/error.html in Redirect. Then, requests for objects such as docs/a.html and docs/b.html are redirected to documents/error.html.
HttpRedirectCode
The HTTP status code returned in the response. The default value is 301, indicating that requests are permanently redirected to the location specified in Redirect. You can change it to another status code if needed.
Redirection rule examples:
- Example 1: All requests for objects prefixed with folder1/ are automatically redirected to pages prefixed with target.html on host www.example.com using HTTPS.
[ { "Condition": { "KeyPrefixEquals": "folder1/" }, "Redirect":{ "Protocol": "https", "HostName": "www.example.com", "ReplaceKeyPrefixWith": "target.html" } } ] - Example 2: All requests for objects prefixed with folder2/ are automatically redirected to objects prefixed with folder/ in the same bucket.
[ { "Condition": { "KeyPrefixEquals": "folder2/" }, "Redirect":{ "ReplaceKeyPrefixWith": "folder/" } } ] - Example 3: All requests for objects prefixed with folder.html are automatically redirected to the folderdeleted.html object in the same bucket.
[ { "Condition": { "KeyPrefixEquals": "folder.html" }, "Redirect":{ "ReplaceKeyWith": "folderdeleted.html" } } ] - Example 4: If the HTTP status code 404 is returned, the request is automatically redirected to the page prefixed with report-404/ on host www.example.com. For example, if you request the page ExamplePage.html but the HTTP 404 error is returned, the request will be redirected to the report-404/ExamplePage.html page on the www.example.com. If no 404 redirection rule is configured, the default 404 error page specified is returned when an HTTP 404 error occurs.
[ { "Condition": { "HttpErrorCodeReturnedEquals": "404" }, "Redirect":{ "HostName": "www.example.com", "ReplaceKeyPrefixWith": "report-404/" } } ]
- Example 1: All requests for objects prefixed with folder1/ are automatically redirected to pages prefixed with target.html on host www.example.com using HTTPS.
- Click OK.
- Verification: In a browser, enter the access domain name of the source object specified in the redirect rule. You are redirected to the target object or URL.
Due to browser caching, you may need to clear your browser cache to see the expected results.
Redirect Default Homepage Requests to Another Object in the Same Bucket or to a URL
To redirect requests for the default homepage of a static website to another object in the same bucket or to a URL, configure static website hosting for the bucket as follows: set Hosting Type to Host a static website, set the default homepage to a static web page file in the bucket's root directory, and add the WebsiteRedirectLocation metadata to that file.
You can configure this setting using OBS Console, APIs, or SDKs, but not using OBS Browser+ or obsutil.
Using OBS Console
Step 1: Configure Static Website Hosting
When configuring static website hosting for a bucket, set Hosting Type to Host a static website and specify the name of the static webpage file in the bucket's root directory as the default homepage.
- (Optional) If the static website files in the bucket are not publicly accessible, configure a bucket policy to grant all users access to the files. If you have already configured the policy, skip this step.
- In the bucket list, find the bucket to redirect requests to and click its name to open the object list page.
- In the navigation pane, choose Permissions > Bucket Policies.
- Click Create.
- Configure the bucket policy.
This step describes only the required key parameters. For other parameters, retain the default settings. For more information, see Granting All Accounts the Read Permission for Certain Objects.
Figure 5 Configuring the read-only permission for objects
Table 5 Bucket policy parameters Parameter
Example
Description
Configuration mode
Visual Editor
Select a policy configuration mode: Visual Editor or JSON.
- Visual Editor: You can configure through a visual interface in an easy way.
- JSON: It lets you configure the policy in code using JSON syntax. For details about the JSON view, see Creating a Custom Bucket Policy (JSON View).
Policy Name
policy-bucket
Enter a custom policy name that is easy to remember.
Effect
Allow
Specifies whether to allow or deny authorized users to perform specific actions on authorized resources under defined conditions.
Principal
All accounts
Select the users who can access the bucket.
You can specify one or more regular accounts or delegated accounts. You must specify regular accounts, delegated accounts, or both.
- All accounts: Grants bucket access to anyone without authentication. This may pose a security risk to your data.
- Current account: Grants bucket access to one or more IAM users in the current account.
- Other accounts: Grants bucket access to other accounts or their IAM users.
- Delegated accounts: Grants bucket access to a delegated account.
Enter the delegating account ID (ID of the account that creates the agency) and the agency name.
For example, if account A creates an agency Agency-A-to-B to authorize account B to operate resources in account A, enter account A's ID as the delegating account ID and Agency-A-to-B as the agency name.
To query the account ID, IAM user ID, delegating account ID, and agency name, see Obtaining Account, IAM User, Project, User Group, Region, and Agency Information.
Resources
Specified objects
Set the object name to *.
Select the resources to which the bucket policy applies.
- Entire bucket (including the objects in it): The bucket policy applies to the bucket and all objects in it.
- Current bucket: The bucket policy applies only to the bucket, excluding objects in it.
- Specified objects: The bucket policy applies to specified objects in the current bucket. Enter * as the object name to apply the bucket policy to all objects in the bucket.
- To match a specific object in the bucket, enter its full name, such as abc.txt.
- To match objects by prefix or suffix, use a wildcard (*). For example, enter a* to match objects whose names start with a or *c to match objects whose names end with c.
- To match objects in a folder, specify the folder name followed by a slash and the matching pattern. For example, testdir/* matches all objects in the testdir folder, testdir/a* matches objects in that folder whose names start with a, and testdir/*c matches objects in that folder whose names end with c.
Actions
Use a template > Object Read-Only
Select the actions that the bucket policy applies to for objects in the bucket.
- Use a template: Use a built-in OBS template to configure actions.
- Object Read-Only: Supports the following actions:
- GetObject: Obtains object content and metadata.
- GetObjectVersion: Obtains the content and metadata of a specific object version.
- GetObjectVersionAcl: Obtains the ACL of a specific object version.
- GetObjectAcl: Obtains the ACL of an object.
- RestoreObject: Restores Archive objects.
- Object Read/Write: Supports the following actions:
- PutObject: Uploads objects using PUT or POST, appends data to objects, initiates multipart uploads, uploads parts, completes multipart uploads, modifies objects, truncates objects, and renames objects.
- GetObject: Obtains object content and metadata.
- GetObjectVersion: Obtains the content and metadata of a specific object version.
- ModifyObjectMetaData: Modifies object metadata.
- ListMultipartUploadParts: Lists uploaded parts.
- AbortMultipartUpload: Aborts a multipart upload.
- GetObjectVersionAcl: Obtains the ACL of a specific object version.
- GetObjectAcl: Obtains the ACL of an object.
- PutObjectAcl: Sets the ACL of an object.
- RestoreObject: Restores Archive objects.
- Object Read-Only: Supports the following actions:
- Customize: Select the actions that the bucket policy applies to for objects in the bucket. For details, see Object Actions.
- Click Create.
The newly created bucket policy appears in the bucket policy list, indicating successful creation.
- In the navigation pane, choose Data Management > Static Website Hosting.
- Click Configure Static Website Hosting. The Configure Static Website Hosting dialog box is displayed.
- Enable Status and configure parameters.
This step describes only the required key parameters. For other parameters, retain the default settings. To configure more parameters, see Configuring Static Website Hosting.
Figure 6 Configuring static website hosting
Table 6 Setting Hosting Type to Host a static website Parameter
Example
Description
Status
Enabled
Specifies whether to enable static website hosting.
Hosting Type
Host a static website
Select Host a static website.
Homepage
index.html
- The landing page that opens by default when the static website is visited. When you configure static website hosting through OBS Console, only HTML webpage files in the bucket's root directory are supported; when you configure it through APIs, this restriction does not apply, but you must specify the object's Content-Type.
OBS supports only files in the bucket root directory, such as index.html, as the default homepage. Files in nested directories, such as /page/index.html, cannot be configured as the default homepage.
- The landing page that opens by default when the static website is visited. When you configure static website hosting through OBS Console, only HTML webpage files in the bucket's root directory are supported; when you configure it through APIs, this restriction does not apply, but you must specify the object's Content-Type.
- Click OK.
Step 2: Add Metadata to the Default Homepage
After configuring a default homepage (an HTML file in the bucket root directory) for static website hosting, you must also add the WebsiteRedirectLocation metadata to the homepage file.
- In the bucket list, click the desired bucket. The Objects page is displayed.
- Click the desired object to open its details page.
- On the Metadata tab, click Add and specify metadata information.
This step describes only the metadata required for this configuration. For more information about object metadata, see Metadata Introduction.
Figure 7 Adding metadata
Table 7 OBS metadata Name
Description
User-Modifiable
WebsiteRedirectLocation
Redirects requests for an object to another object or an external URL. When redirection is configured for an object, requests for that object return a response containing the WebsiteRedirectLocation header to tell the client which target URL to redirect to. The redirection is implemented through static website hosting.
The format is WebsiteRedirectLocation: <url>, where <url> indicates the target URL to redirect requests to.
OBS supports redirection only for objects in the bucket's root directory, not for objects within folders.
For example, you can redirect requests for an object by doing as follows:
- Configure the WebsiteRedirectLocation metadata for object testobject.html in the root directory of bucket testbucket and set the metadata value to http://www.example.com.
- Configure static website hosting for bucket testbucket, and set object testobject.html as the default homepage.
- Access object testobject.html through the hosted URL provided on the static website hosting page. The access will be redirected to https://www.example.com.
Yes
- Click OK.
Step 3: Verify the Configuration
Enter the hosted URL provided on the static website hosting page in a browser to access the default homepage. The request will be redirected to the target (which can be another object in the same bucket or a URL) specified by the WebsiteRedirectLocation metadata of the default homepage.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot
