# Host a static website

Serve a bucket's public objects as a static website at https://<bucket>.s3-website.<domain>, with an index and an error document.

Replace _example-site_ with your bucket's name. The website serves only objects the bucket policy
makes public.

## Prerequisites

- Permission for `s3:PutBucketWebsite` on the bucket.
- In the console, also `s3:GetBucket` on the bucket to open it (without it the bucket's page says
  "Access denied").
- The bucket's objects are public (see [Make objects public](make-objects-public.md)). Without
  that, every page answers `403`.
- The bucket is not one the platform writes.

## Step 1: Turn the website on

**Console**
1. Open **Object Storage**, open the bucket, then choose **Website**.
2. Under **Turn on**, choose **Serve the bucket’s files**.
3. For **Index document**, keep `index.html` or enter another name.
4. (Optional) For **Error document**, enter `404.html`.
5. Choose **Turn on website**.

The console says "Website on." and **Status** shows **On**, or **On, not public** with the
banner "Every page answers 403: nothing in this bucket is public yet." while nothing in the bucket
is public. Until the index or error document is uploaded, a banner says it is not in the bucket
yet.

**CLI**
```sh
dcloud s3 bucket-access set-website example-site --index-document index.html --error-document 404.html
```

**API**
```http
PUT /s3/api/buckets/example-site/website
{ "indexDocument": "index.html", "errorDocument": "404.html" }
```

**Terraform**
```hcl
resource "dcloud_bucket_website" "site" {
  bucket         = dcloud_bucket.site.id
  index_document = "index.html"
  error_document = "404.html"
}
```

Over S3: `aws --profile dcloud --endpoint-url https://s3.<domain> s3 website s3://example-site/ --index-document index.html --error-document 404.html`.

## Step 2: Upload the site

```sh
aws --profile dcloud --endpoint-url https://s3.<domain> s3 sync ./public s3://example-site/ --delete
```

The AWS CLI sets each file's content type from its extension.

## Result

The website answers at `https://example-site.s3-website.<domain>` (the console's **Address**). Its
certificate is issued on the first visit, in a few seconds.

| Request         | Answer                                                                                        |
| --------------- | --------------------------------------------------------------------------------------------- |
| `/`, `/docs/`   | `index.html`, `docs/index.html`                                                               |
| `/docs`         | `302` to `/docs/` when `docs/index.html` exists and is public                                 |
| a missing page  | the error document with status `404` when it exists and is public; otherwise a plain 404 page |
| a private key   | `403`: only objects the bucket policy makes public are served                                 |
| not GET or HEAD | `405`                                                                                         |

To send every request to another host instead, see
[Redirect website requests](redirect-website-requests.md). For your own domain, see
[Use a custom domain for a website](use-a-custom-domain.md).

## Turn the website off

Choose **Turn off** on the **Website** tab, run
`dcloud s3 bucket-access delete-website example-site` (`s3:DeleteBucketWebsite`), or
destroy the `dcloud_bucket_website`; custom domains are kept.

## If it is refused

- `400` naming the problem, such as `An index document is required` or
  `The index document may not contain a slash`.
- `403` `This bucket is managed by the platform; its access cannot be changed` for a bucket the
  platform writes; the console says `A bucket the platform manages cannot be a website.`
- `403` `Forbidden` without `s3:PutBucketWebsite`; the console's controls are unavailable and say
  `Needs s3:PutBucketWebsite on this bucket.` (**Turn off** says `Needs s3:DeleteBucketWebsite.`)

## Related

- [Make objects public](make-objects-public.md)
- `s3.bucket-access.set-website` · `dcloud s3 bucket-access set-website` · `dcloud_bucket_website`
