14

Is there some way to map multiple (thousands) of subdomains to one s3-bucket? If so is it also possible to map it to a specific path in the bucket for each subdomain?

I want test1.example.com to map to mybucket/test1 and test2.example.com to map to mybucket/test2.

I know the last part isn't possible with normal dns-records but maybe there is some nifty Route 53 feature?

2 Answers 2

16

It's not possible with S3 directly. You can only use 1 subdomain with an S3 bucket.

However you can map multiple subdomains to a Cloudfront distribution.

Update (thanks to @SimonHutchison's comment below)

You can now map up to 100 alternate domains to a Cloudfront distribution - see http://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html#limits_cloudfront

You can also use a wildcard to map any subdomain to your distribution:

Using the * Wildcard in Alternate Domain Names

When you add alternate domain names, you can use the * wildcard at the beginning of a domain name instead of specifying subdomains individually. For example, with an alternate domain name of *.example.com, you can use any domain name that ends with example.com in your object URLs, such as www.example.com, product-name.example.com, and marketing.product-name.example.com. The name of an object is the same regardless of the domain name, for example:

www.example.com/images/image.jpg

product-name.example.com/images/image.jpg

marketing.product-name.example.com/images/image.jpg

4
  • 1
    @Martin - instructions are in my link to the Cloudfront docs. I've added an extract in my answer. Jun 7, 2011 at 8:08
  • Can you have an Amazon S3 bucket accessed by "img.domain1.com" and "img.domain2.com" ? How would that increment the cost?
    – vtortola
    Feb 5, 2014 at 23:42
  • @vtortola, the answer is yes. It's done Amazon CloudFront. Which is what this answer is describing.
    – Michael
    May 21, 2014 at 15:30
  • 1
    @GeoffAppleford I know this is super old, but people are clearly hitting this on google, if you have a second can you pls update answer? You can now add 100 CNAME records directly in the AWS cloudfront distribution. No need to go to Route53 at all. Sep 1, 2016 at 6:12
5

Starting from October 2012 Amazon introduced a function to handle redirects (HTTP 301) for S3 buckets. You can read the release notes here and refer to this link for configuration via Console / API.

From AWS S3 docs :

  • Redirects all requests If your root domain is example.com and you want to serve requests for both http://example.com and http://www.example.com, you can create two buckets named example.com and www.example.com, maintain website content in only one bucket, say, example.com, and configure the other bucket to redirect all requests to the example.com bucket.

  • Advanced conditional redirects You can conditionally route requests according to specific object key names or prefixes in the request, or according to the response code. For example, suppose that you delete or rename an object in your bucket. You can add a routing rule that redirects the request to another object. Suppose that you want to make a folder unavailable. You can add a routing rule to redirect the request to another page, which explains why the folder is no longer available. You can also add a routing rule to handle an error condition by routing requests that return the error to another domain, where the error will be processed.

2
  • Thank you! This is absolutely correct and should be updated as the correct answer given the changes to S3 buckets since the question was asked. Obviously the CloudFront solution will work, but this is perfect if you don't want to use a CDN.
    – Nicholas
    Jan 21, 2018 at 23:36
  • this would work, but change the actual URL. so you still cannot have two different domains pointing to the same bucket content, if you want the user to see two different domain Apr 29, 2019 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.