Fairly often we purchase a new domain or decommission an existing one and need to redirect all queries to it to a specific URL on a wholly different domain and website. Practically all services where you can register domain like GoDaddy, name.com and Namecheap offer a redirect function, but nearly all fail to work if the user attempts to go to a HTTPS URL on the domain you are redirecting.
If you’re already making use of Amazon AWS Cloud, a very low cost and foolproof solution is to utilize Amazon S3’s static website hosting feature along with Amazon CloudFront to handle the redirect:
- Create a new Amazon S3 bucket (eg. domain-redirect-olddomaincom) with ACLs disabled, the checkbox to block all public access unchecked (as we wish to allow public access) and bucket versioning disabled; create the bucket.
- Edit the newly created S3 bucket; under the Properties tab, edit the Static Website Hosting properties.
- In the Static Website Hosting properties page:
- Static website hosting: Enable
- Hosting type: Host a static website
- Index document: index.html
- Redirection rules: Copy/paste the following, with the appropriate changes made to the values of HostName and ReplaceKeyPrefixWith:
[ { "Redirect": { "HostName": "www.newdomain.com", "HttpRedirectCode": "301", "Protocol": "https", "ReplaceKeyPrefixWith": "path/to/redirect/to/" } } ]
- Hit the Save Changes button to finalize the redirect using the S3 bucket; it should work immediately and allow you to test it via the Bucket Website Endpoint URL shown on the page.
- Create a new Amazon CloudFront distribution, making sure to:
- Create Origin: Ensure that it points to the static website endpoint of your new S3 bucket (eg. domain-redirect-olddomaincom.s3-website.ca-central-1.amazonaws.com, not the default of domain-redirect-olddomaincom.s3.ca-central-1.amazonaws.com)
- Alternate domain name (CNAME): Enter all versions of the domain you wish to be handled and redirected (eg. olddomain.com and www.olddomain.com)
- Custom SSL certificate: Request a certificate that handles the domains you entered above; this is the (free) option that allows your newly set up domain redirect to respond properly to both HTTP and HTTPS requests
- Add the Amazon CloudFront domain (eg. d1gyvh82u10kd6.cloudfront.net) to your domain name’s DNS records for the root and subdomains you wish to redirect.
You’re done! While a bit long-winded as a process, this appears to be the cheapest and most foolproof way to set up a domain redirect for the long term.