I'm using Amazon Simple Storage Service (Amazon S3) static website redirection rules. Amazon S3 adds a slash at the end of the URL that breaks my website.
Short description
Note: The following resolution applies only to Amazon S3 static website endpoints. For more information, see Website endpoints.
Amazon S3 might add a trailing slash when it interprets a request as a directory-style prefix. For example, if path/index.html exists and the index document is index.html, then a request to /path can receive an intermediate redirect to /path/. This behavior is expected for Amazon S3 static website endpoints. Amazon S3 doesn't add a trailing slash when you use bucket-level redirect requests for an object. The following table shows typical outcomes of redirection methods:
| Method | Example request | S3 adds "/" | Typical outcome |
|---|
| Redirect requests for an object | /path | No | https://target/path |
| Routing rules with a directory index | /path | Yes | /path/ then redirect |
| Redirect metadata on index object | /path | Yes | /path then /path then https://target/path |
| Exact key redirect object path | /path | No | Single hop to https://target/path |
To avoid the addition of a trailing slash in the redirected URL, use the following workaround:
- Configure an index document to host the S3 static website.
- Set the website redirect location in the metadata of the object to configure the redirect.
Resolution
Configure an index document to host the static website
First, complete the following steps to specify the name of the index document, and delete the redirection rules:
- Open the Amazon S3 console.
- From the Buckets list, choose the name of your bucket.
- Choose Properties.
- Under Static website hosting, choose Edit.
- On the Edit Static website hosting page, for Static website hosting, choose Enable.
- For Hosting type, select Host a static website.
- For Index document, specify the name of an object that you want to use to set the redirection metadata. An example name is, index.html.
Note: Don't begin your index name with a "/". For example, don't use: /index.html. Also, Amazon S3 object names are case sensitive.
- Delete the redirection rules.
- Choose Save changes.
Then, use the S3 console to configure the index document.
Set up the website redirection
Complete the following steps:
- Open the Amazon S3 console.
- From the Buckets list, choose the name of your bucket.
- Under Objects, choose your index document.
- Choose the Properties tab.
- In the Metadata section, choose Edit.
- Under Metadata, do the following:
Choose Add metadata.
For Type, select System defined.
For Key, choose x-amz-website-redirect-location from the dropdown list.
For Value, enter the redirect URL.
- Choose Save changes.
Note: If you use this workaround, then Amazon S3 still adds a trailing slash to the redirected URL when it interprets the request as a directory. However, the final redirect to your target URL omits the trailing slash.
Verify the results
To verify that your redirect works, submit curl requests with and without the trailing slash. Confirm that the final redirect location is accurate. For example, run the following commands:
curl -L -I http://yourdomain/path
curl -L -I http://yourdomain/path/
Note: Replace yourdomain/path with the domain name and path for your URL.
Related information
Configuring a webpage redirect