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
andwww.ourcooldoma.in
. - we are already using
AWS
.
(Told you it was specific..)
Then we can do this:
- Set up an empty S3 bucket at AWS and make it public.
- 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. - Because we support HTTPS use
ACM
to create a certificate that matchesourcooldoma.in
andwww.ourcooldoma.in
(or*.ourcooldoma.in
). - Create a Cloudfront distribution that uses that bucket’s Endpoint as origin and is valid for the domains
www.ourcooldoma.in
andourcooldoma.in
. Use the certificate from the step before. - Configure
ALIAS
records at Namecheap forwww
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.