What
What is Apex domain ?
When you buy a domain, the naked domain name is called the apex domain. where you may add other sub domain to. EX - you buy domainname.com, the domainname.com is the apex domain. and the sub domains can be blog.domainname.com .
But you dont get the domain to create sub doomains. Sooner or later you will want to connect this apex domain to a site as well. And thats where you will face an issues. This blog shows you how to fix that.
Why
When you have a domain with your dns registrar and have a site ready to be served using this domain. you will have to redirect the server where the site is hosted. Now if you have a single server its great. you put ip into the A record for the domain name.
But what if you have a cloudfront or load balancer or s3 hosting. anything that have a domain name not a single ip? you will now want to add a CNAME. DNS does not allow you to add CNAME to the apex domain. and you are stuck.
How
To fix this you will have different methods provided by different domain registrar.
Add Web Redirects
Some will provide an option to add a web redirect record into their dns records. this will in the backend have a proxy running that will redirect the request from apex domain to the domain you want.
This can also be done by you manually, but now you have a server running with a proxy for just to redirect things. web recirect records makes this thing easy.
Here you may get issues with certificates sometimes. but good to go if you have this feature.
Using Route 53 with S3
If you are at AWS route 53 for dns, you will have option to add A record to the s3.
Interested ! let get deeper.
Get an s3 bucket ready with the name of the apex domain name. make this public. goto the static site hosting part and enable this.
Now for the hosting type set to the > Redirect requset for an object.
Now goto the route 53 and then set this s3 as the target in the A record for the apex domain. Go test this. satisfied ?
Try with https for your domain name.
Yeah, that was bad. the above approah only works with http not https. how to fix this ? first thing you need to do, read further.
Using cloudfront
For this you need to have a cloudfront distribution ready for the site that you want to connect to the apex domain. and instead of connecting the s3 to the A record for the apex domain, you connect the cloudfront to the A record. and you are done.
Now go check. offcourse you need to wait for the dns to propegate. but after that https should be working fine. and you are done.
Thoughts
you saw this can be done using multiple ways. things will change when you know what actually you want and your way to solve this might change. what you want is to redirect
http://domainname.com
https://domainname.com
http://www.domainname.com
https://www.domainname.com
the above 4 name to a single target. as long as you can achieve this which is less resource consuming and faster you ar good.