13

We've created a subdomain via Route 53 on AWS that points to an external server. We now want to redirect that traffic back to the main domain, but to a subdirectory.

shop.mydomain.com would redirect to mydomain.com/shop

DNS doesn't like resolving subdirectories like that... any suggestions?

secretmike
  • 9,814
  • 3
  • 33
  • 38
sea26.2
  • 376
  • 1
  • 5
  • 23

1 Answers1

18

If I understand correctly, you want ALL requests to shop.mydomain.com to redirect to mydomain.com/shop.

In that case I recommend using S3's "Redirection Rules".

Create a bucket in S3 called shop.mydomain.com. Open the bucket properties and set the bucket to "Enable Website Hosting". Then select the "Edit Redirection Rules" option.

AWS Console Edit Redirection Rules Screenshot

We now need to define a redirection rule that matches every request, and redirects it to mydomain.com/shop

<RoutingRules>
  <RoutingRule>
    <Redirect>
      <Protocol>https</Protocol>
      <HostName>mydomain.com</HostName>
      <ReplaceKeyPrefixWith>shop/</ReplaceKeyPrefixWith>
      <HttpRedirectCode>301</HttpRedirectCode>
    </Redirect>
  </RoutingRule>
</RoutingRules>

This rule will match every request and redirect it.

For more info on routing rules, check out the official docs: http://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html#configure-bucket-as-website-routing-rule-syntax

Now, you need to configure shop.mydomain.com in Amazon Route 53 as an ALIAS record pointing at the bucket you just created, shop.mydomain.com.

That's it - Enjoy!

secretmike
  • 9,814
  • 3
  • 33
  • 38
  • 1
    Is there any way to have an Amazon S3 bucket's subfolder redirect to an external server? E.g. I'm trying to get mydomain.com/blog to forward to an external Wordpress server but KEEP the domain as mydomain.com/blog. I know this can be done with a subdomain but I'd prefer to use a subfolder. I have my Wordpress on another separate server because S3 hosting doesn't support WP (CGI/PHP/etc.) – evolross Oct 16 '15 at 20:42
  • 5
    I believe your best bet is to use CloudFront. It supports different routing rules so you can points `mydomain.com/blog` to your wordpress blog, and everything else to your S3 bucket. Since cloudfront proxies requests, your users won't see any separation - everything will look like a single site under mydomain.com – secretmike Oct 17 '15 at 02:43
  • @secretmike This sounds like an interesting option. Can you post more information about how to do this, or perhaps links to a suggested solution? – modulitos Mar 16 '16 at 08:50
  • I would also note that I was required to fill out the `Index Document:` field. Since I was trying to redirect to a folder with no specific document (ie `example.com/sub/folder/`), I just entered `index.html` and it worked – modulitos Mar 16 '16 at 08:56
  • I think [this link regarding origins and behaviors](http://stackoverflow.com/questions/31567994/multiple-cloudfront-origins-with-behavior-path-redirection?rq=1) can also help. Just to emphasize the suggested solution, it's based on the fact that you can define several sources (ELB wrapped around some EC2, S3) that co-exists and pick between them with several behaviors acting as routing. – Daniel Dror Aug 07 '16 at 06:46
  • What I got doing that was the redirect to `mydomain.com/shop/shop/shop/shop/shop/shop/shop/shop`. – R. Karlus Oct 15 '20 at 00:29