1

I have a Flask app that I use Zappa to deploy. I have successfully deployed it to www.my_domain.com . But a lot of folks have told me "your site's down" and when I followed up it's because they go to https://my_domain.com (no www).

I'd like to redirect from that to the working site. My certificate is via AWS and technically for www.my_domain.com, *.my_domain.com, and my_domain.com (though when I did the email-based approval *.my_domain.com and my_domain.com both were interpreted as my_domain.com).

I've tried changing my zappa_settings.json domain but no change.

davidism
  • 98,508
  • 22
  • 317
  • 288
ScottieB
  • 3,617
  • 6
  • 34
  • 57

2 Answers2

1

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

  1. In the AWS Console, go to the API Gateway service
  2. In the left hand menu, click on Custom domain names
  3. Click on your domain (www.my_domain.com)
  4. Click on API mappings over on the left and make note of the settings for API and Stage
  5. Click on the Create button on the left
    1. Enter the bare domain name (e.g. my_domain.com)
    2. Select Edge optimized
    3. Under ACM Certificate, select the one with the name of the domain you created
    4. Click on Create domain name
  6. Click on the newly created bare domain (my_domain.com)
  7. Select API mappings
  8. Click on Configure API Mappings
    1. Add the same mappings you took note of earlier
    2. Click on Save

Route 53 configuration

  1. In the AWS Console, go to the Route 53 service
  2. On the left menu, select Hosted Zones
  3. Select your domain (e.g. my_domain.com)
  4. Click Create Record
    1. In Record Name, put in nothing
    2. In Record Type, select A record (CNAME if you are doing the reverse)
    3. In Value, type the www domain name (www.my_domain.com)
    4. In TTL, you can put whatever you want but 172800 will cache requests for two days, which is not too bad.
    5. 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.

Edgar
  • 851
  • 7
  • 8
  • "Tried to create resource record set but it already exists". Tried aliasing, but same error. Sure enough, there's a CNAME for www, and NS and SOA records for the prefixless. If I try modifying the existing one by adding `my_domain.com` in a new line after the cloudfront address, I get a different error: "RRSet of type CNAME with DNS name www.my_domain.com. does not contain exactly one resource record." – ScottieB Feb 08 '21 at 03:29
0

I followed this thread.

Here's the steps that worked (though I'm not sure why):

  • Go to API Gateway
  • Create a new custom domain: my_domain.com
  • Use the records for previously-approved my_domain.com
  • In the new domain, under "API mappings" copy the settings from www.my_domain.com
  • back to Route 53
  • create an A record
  • leave the prefix blank
  • copy over the cloudfront url in "Route traffic to" from the new API Gateway domain
ScottieB
  • 3,617
  • 6
  • 34
  • 57
  • I think it works because the CF distro needs to properly terminate SSL based on the domain name. So a simple CNAME is not enough (without HTTPS it would have been). So so when you create a new custom domain, you allow it to terminate SSL for the new domain. Make sure when you request the certificate you add both www and bare versions of the domain. – Edgar Feb 12 '21 at 15:21