Updated on 2026-09-22 GMT+08:00

Setting Website Hosting

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.

You can call ObsClient->setBucketWebsite to set website hosting for a bucket.

Configuring the Default Homepage and Error Page

Sample code:

<?php

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([ 
    // Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    'key' => getenv('ACCESS_KEY_ID'),
    'secret' => getenv('SECRET_ACCESS_KEY'),
    'endpoint' => 'https://your-endpoint',
]);

try {
    // Configure static website hosting for a bucket.
    $resp = $obsClient->setBucketWebsite([ 
        'Bucket' => 'bucketname',
        // Configure the default homepage.
        'IndexDocument' => ['Suffix' => 'index.html'],       
        // Configure the error page.
        'ErrorDocument' => ['Key' => 'error.html']
    ]);

    printf("RequestId:%s\n",$resp['RequestId']);
} catch (ObsException $e) {
    // If the operation fails, obtain the exception details.
    printf("SetBucketWebsiteFailed: Status:%d, Code:%s, Message:%s\n",
         $e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
    // Close ObsClient to release resources.
    $obsClient->close();
}

Configuring Redirection Rules

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([ 
    // Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.    'key' => getenv('ACCESS_KEY_ID'),
    'secret' => getenv('SECRET_ACCESS_KEY'),
    'endpoint' => 'https://your-endpoint',
]);


try {
    // Configure static website hosting for a bucket.
    $resp = $obsClient->setBucketWebsite([ 
       'Bucket' => 'bucketname',
       // Configure the default homepage.
       'IndexDocument' => ['Suffix' => 'index.html'],       
       // Configure the error page.
       'ErrorDocument' => ['Key' => 'error.html'],
       // Configure redirection rules.
       'RoutingRules' => [                                    
             ['Condition' => ['HttpErrorCodeReturnedEquals' => 404], 'Redirect' => ['Protocol' => 'http', 'ReplaceKeyWith' => 'NotFound.html']],                     
             ['Condition' => ['HttpErrorCodeReturnedEquals' => 404], 'Redirect' => ['Protocol' => 'https', 'ReplaceKeyWith' => 'index.html']]
       ]
    ]);

    printf("RequestId:%s\n",$resp['RequestId']);
} catch (ObsException $e) {
    // If the operation fails, obtain the exception details.
    printf("SetBucketWebsiteFailed: Status:%d, Code:%s, Message:%s\n",
         $e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
    // Close ObsClient to release resources.
    $obsClient->close();
}

Use the RoutingRules parameter to specify redirection rules for a bucket.

Configuring Redirection for All Requests

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([ 
    // Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.    'key' => getenv('ACCESS_KEY_ID'),
    'secret' => getenv('SECRET_ACCESS_KEY'),
    'endpoint' => 'https://your-endpoint',
]);

try {
    // Configure static website hosting for a bucket.
    $resp = $obsClient->setBucketWebsite([ 
       'Bucket' => 'bucketname',
       'RedirectAllRequestsTo' => ['HostName' => 'www.example.com', 'Protocol' => 'http']
    ]);

    printf("RequestId:%s\n",$resp['RequestId']);
} catch (ObsException $e) {
    // If the operation fails, obtain the exception details.
    printf("SetBucketWebsiteFailed: Status:%d, Code:%s, Message:%s\n",
         $e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
    // Close ObsClient to release resources.
    $obsClient->close();
}

Use the RedirectAllRequestsTo parameter to set redirection rules for all requests for accessing a bucket.