Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Question: How to redirect www to non-www with AWS #633

Closed
alan345 opened this issue Sep 28, 2020 · 7 comments
Closed

Question: How to redirect www to non-www with AWS #633

alan345 opened this issue Sep 28, 2020 · 7 comments

Comments

@alan345
Copy link

alan345 commented Sep 28, 2020

With AWS, Can you guide me to redirect www to non-www to avoid duplicate content?
Should we create a s3 bucket and a new Cloud front distribution? (https://www.pbxdom.com/how-redirect-non-www-to-www-with-amazon-cloud-front-route-53-and-s3)

or, can we set it up in the serverless.yml file?

Thanks!

@alan345 alan345 changed the title Question: How to redirect www to non-www to avoid duplicate content with AWS Question: How to redirect www to non-www with AWS Sep 28, 2020
@dphang
Copy link
Collaborator

dphang commented Sep 28, 2020

I think you can follow here for now: https://github.com/serverless-nextjs/serverless-next.js#i-was-expecting-for-automatic-subdomain-redirection-when-using-the-domaintype-wwwapex-input. Basically you need to manually create new S3 bucket and CloudFront distribution (for HTTPS redirect) and some new Route53 A record.

In the future it's possible to support it in this component within the same CF distribution, but we would need to make a couple of changes:

  • Support multiple domain inputs mapped to one distribution (so you can have www.example.com and example.com Route53 records pointing to same CF distribution). EDIT: I think this is not needed since I think with domainType: both input it can create both www and non-www (apex) domain. But if you want other redirects that's not from www to non-www (e.g abc.example.com -> example.com) currently this doesn't look supported unless you also use Terraform to manage domain mappings.
  • Lambda could return a redirect response from www.example.com -> example.com (or vice versa)

@dphang
Copy link
Collaborator

dphang commented Sep 29, 2020

Now that redirects are done (#587 (comment)) we could leverage similar code to do a redirect from a domain name to another in the Lambda@Edge itself. Probably create another input like domainRedirects that lets users specify where they want to redirect from e.g from www.example.com to example.com.

Although it's just a bit costlier than the S3 bucket redirect (due to use of Lambda@Edge compute), it has a few advantages:

  • The redirect is handled at the edge server instead of at a single point (e.g S3 bucket in a single region). This should have lower latency for most users.
  • Easier to manage within this component; no need to setup two separate CF distributions and another bucket.

Caveat: currently only the default cache behavior (/*) have at Lambda@Edge association, so you won't be able to redirect static files under /_next/static/*. We could solve in the future by adding another handler for this cache behavior, but probably not worth it just for redirects.

@alan345
Copy link
Author

alan345 commented Sep 29, 2020

thanks for your update. I have as recommended manually create new S3 bucket and CloudFront distribution and some new Route53 A record.

I have 2 CloudFront distribution :

It works well.

Now, if I deploy again via npx serverless I have this error:
nextApp › CNAMEAlreadyExists: One or more of the CNAMEs you provided are already associated with a different resource.

I just removed domain in serverless.yml for now to avoid having this issue.

@dphang
Copy link
Collaborator

dphang commented Sep 29, 2020

Yeah, I think you may need to set domainType input to apex only since www.example.com is already associated with your CF distribution for redirection. Let me know if that works.

@dphang
Copy link
Collaborator

dphang commented Oct 7, 2020

I've released the PR: #639 which can support this domain-level redirect in the Lambda@Edge itself. If you wish, you can try this method - please refer to the README for more details.

@dphang dphang closed this as completed Oct 7, 2020
@pecoram
Copy link

pecoram commented Oct 12, 2020

@dphang which version of the component should be used to do that?

@dphang
Copy link
Collaborator

dphang commented Oct 12, 2020

@pecoram it should be in 1.18.0-alpha.1. You can check the commit for the git tag for @sls-next/serverless-component a12e31a (I know it's a bit confusing with the many tags, we will work on improving the release changelogs soon).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants