For purposes of this answer, I'm assuming that the bare domain is the secondary and the www
is the primary. If you choose the opposite, simply reverse the terms while reading.
Redirect one site to the other
Redirection means that the browser can load my_domain.com
but receives the
HTTP response of 301 moved permanently.
This means that the browser is redirected to www.my_domain.com
and navigates to that site.
The user sees the address bar changed to be www.my_domain.com
(although many browsers now hide
the www
part of the address), and new page loads.
Redirection requires some type of HTTP server running at my_domain.com
. If you already have
an HTTP server running, then it's pretty straightforward to configure the web server to send the redirect.
If you don't have a server, then you can use a static web site service like S3, Github, Netlify to
provide the redirect.
It is kind of annoying to have to set up an entire web server to just send a 301 code. But this method
is well understood and well supported.
Alias one site to the other
Alias means that both my_domain.com
and www.my_domain.com
work and use only one Zappa project.
It's like two doors that go into the same room.
The browser's address bar doesn't change and the user operates on the site normally. One caveat of
this approach is to be a little more careful if you are using cookies to make sure they are shared
between the two domains.
Aliasing requires some advanced configuration of AWS services.
API Gateway configuration
- In the AWS Console, go to the API Gateway service
- In the left hand menu, click on Custom domain names
- Click on your domain (
www.my_domain.com
)
- Click on API mappings over on the left and make note of the settings for API and Stage
- Click on the Create button on the left
- Enter the bare domain name (e.g.
my_domain.com
)
- Select
Edge optimized
- Under ACM Certificate, select the one with the name of the domain you created
- Click on Create domain name
- Click on the newly created bare domain (
my_domain.com
)
- Select API mappings
- Click on Configure API Mappings
- Add the same mappings you took note of earlier
- Click on Save
Route 53 configuration
- In the AWS Console, go to the Route 53 service
- On the left menu, select Hosted Zones
- Select your domain (e.g. my_domain.com)
- Click Create Record
- In Record Name, put in nothing
- In Record Type, select
A
record (CNAME
if you are doing the reverse)
- In Value, type the
www
domain name (www.my_domain.com
)
- In TTL, you can put whatever you want but
172800
will cache requests for two days, which is not too bad.
- Click Create records
ACM configuration
You'll have to ensure that the certificate covers both my_domain.com
and www.my_domain.com
.
Zappa configuration
Make sure to add both domain names in ALLOWED_HOSTS
otherwise Django will reject the request.