Redirect subdomain URL to URL subdirectory on Amazon AWS

sea26.2 picture sea26.2 · Jul 18, 2013 · Viewed 11.3k times · Source

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?

Answer

secretmike picture secretmike · Jul 22, 2013

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!