Lukas Z's Blog

How to Redirect HTTP and HTTPS Requests Using Namecheap and AWS

This is a mini writeup for a very specific case.

Let’s say we have a domain name, ourcooldoma.in. And for some reason we want to redirect all traffic to somecool.org. And the following things also apply:

  • ourcooldoma.in is registered at a hoster like Namecheap.
  • somecool.org does not belong ot us at all.
  • we want to redirect ourcooldoma.in and www.ourcooldoma.in.
  • we are already using AWS.

(Told you it was specific..)

Then we can do this:

  1. Set up an empty S3 bucket at AWS and make it public.
  2. In the properties of the bucket use the Static Website Hosting feature in Properties and define a redirect to somecool.org. Take note of the Endpoint value in the Static Website Hosting box.
  3. Because we support HTTPS use ACM to create a certificate that matches ourcooldoma.in and www.ourcooldoma.in (or *.ourcooldoma.in).
  4. Create a Cloudfront distribution that uses that bucket’s Endpoint as origin and is valid for the domains www.ourcooldoma.in and ourcooldoma.in. Use the certificate from the step before.
  5. Configure ALIAS records at Namecheap for www and @ pointing to the domain name of our Cloudfront distribution.

Done!

Now every request to ourcooldoma.in will result in a DNS query that returns the ALIAS entry (though I believe ALIAS is not a standard key but a technicality provided by Namecheap and others to circumvent the restriction of CNAME, which one would normally use, but which cannot be used with @ unless you are ok with masking your MX and other subdomain records).

So our request will be actually fired against the IP address at Cloudfront but the client still expects a valid SSL certificate for ourcooldoma.in. Cloudfront provides that (created in step 3) and we can finally get to the (cached) bucket, which only does one thing: 301 redirect the client to somecool.org.

P.S.: You can follow me on Twitter.

Comments

Webmentions