When you have a website URL that is lengthy and difficult to remember, the first thing you may think of is to bookmark it. However, after you bookmarked so many of them, hunting for a website among a large list of bookmarks is still a pain. There is another way. You can set up a short and easy-to-remember domain name to redirect to the web URL.
Web Redirect vs CNAME
Before we start the hands-on, let’s have a quick look at the differences between Web Redirect and CNAME.
How CNAME works is like pointing two domain names to the same web server. For example, assume you’ve already mapped domainA.com to your web server’s IP address. If you also want to point domainB.com to the same website server, you can set domainB.com’s CNAME to be domainA.com. As a result, by opening either domain names in your browser, you will see the same website.
There is a limitation with the CNAME though. If you want to point domainB.com to a particular directory of domainA.com, e.g. http://domainA.com/directoryA/pageA.html, CNAME can’t do it. This is where the web redirect can help.
How web redirect works is like you come to website A, it tells you “sorry mate, I’ve moved. This is the new address – website B”. We can see that the web redirect is achieved via a middle-man website instead of DNS settings. So you almost need two websites, one redirects to another. In AWS’s context, using just Route 53 is not enough. And we don’t want to spin up an EC2 just to do the redirect job. Luckily, we have Static Website Hosting from S3 as a cost-effective option. The diagram below demonstrates how it works.
The static website configured in S3 is acting as the middle man. The Route 53’s job is to direct all access traffic to the middle man. The middle man then redirects the traffic to the final destination. We’ll show you how to do this web redirect using AWS Route 53 + S3 below.
The hands-on case we’ll demonstrate is to redirect domainB.com to http://domainA.com/directoryA/pageA.html. To set up this web redirect, there are a few prerequisites:
- you have sufficient permission to configure Route 53 and S3 in an AWS account
- you’ve delegated DNS management of domainB.com to Route 53 in this account*
* You can achieve the DNS Delegation by either registering the domainB.com directly in AWS Route 53, or point this domain’s NS records to AWS name servers. We’ll show you how to delegate a domain name’s DNS management in a separate article.
Once you have the prerequisites met, you can follow the steps below to configure the web redirect.
Set up S3 bucket
- Log in to the console of your AWS account, and navigate to the S3 console page
- Click the Create Bucket button to bring up the Create Bucket wizard page
- Enter the redirect-from domain name in the Bucket name field, in our case, domainB.com
- Choose the AWS Region that suits your geolocation, keep the rest settings as default, and then click the Create bucket button at the bottom of the page.
- Once the bucket is created, click the bucket name from the bucket list, and then click the Properties tab.
- On the Properties page, click the Edit button in the Static website hosting panel.
- Select the Enable radio button, and then select Redirect requests for an object
- In the Host name field, enter the redirect-to URL, in our case, domainA.com/directoryA/pageA.html
Note, don’t put http:// or https:// in front of the URL. The protocol should be specified at step 9 below.
- The Protocol setting is optional, e.g. if you want to route all traffic on to an SSL connection, you can choose https.
- Click the Save changes button.
Now you should see a Bucket website endpoint displayed in the Static website hosting panel. In our case, it’s http://domainB.com.s3-website-ap-sourtheast-2.amazoneaws.com. We need to note down the part that’s highlighted in red. It’ll be required in Route 53 configuration.
Set up Route 53
- Navigate to the Route 53 console page, and then click the redirect-from domain name from the list, in our case domainB.com.
- On the hosted zone details page, click the Create record button.
- On the Quick create record page, select A-Routes traffic to an IPv4 address and some AWS resources in the Record type field.
- Switch on the Ailias button, and then select Alias to S3 website endpoint in the Route traffic to field.
- Select the same region as you configured in your S3 bucket (ref step 4 in S3 bucket configuration)
- Paste the endpoint that you’ve noted down at the end of S3 bucket configuration
- Select Simple routing in the Routing Policy field, and set No for Evaluate target health.
- Click the Create records button.
Usually AWS Route 53 will propagate DNS records changes to its world-wide DNS servers within 60 seconds. If you Once it’s ready, your web redirect from domainB.com to http://domainA.com/directoryA/pageA.html will start to take effect. From now on, you can just use the branded domain name to access your mapped web page. You can still use the orginal lengthy URL to access the same page, but why bother?
For more information about AWS S3 static web hosting, see Hosting a static website using Amazon S3.