Contact Us

Full Guide: Redirecting www to non-www in AWS

Maintaining a consistent URL structure is crucial for website management and search engine optimization (SEO). This guide explores five methods to redirect the "www" subdomain to the root domain (e.g., from www.cloudplexo.com to cloudplexo.com) using AWS services.

SEO Implications of www vs. non-www Redirects

The choice between www and non-www URLs, and implementing proper redirects, can significantly impact your website's SEO:

When implementing any of the following redirection methods, ensure they result in proper 301 redirects to maximize SEO benefits. Also, after implementation, update your Google Search Console and other webmaster tools to reflect your preferred domain.

1. Amazon S3 and CloudFront

Implementation:

  1. Create two S3 buckets: one for the root domain (example.com) and another for the www subdomain (www.example.com).
  2. Configure the www bucket for static website hosting with a redirect to the root domain.
  3. Create a CloudFront distribution for the root domain.
  4. Set up another CloudFront distribution for the www subdomain, pointing to the www S3 bucket.
  5. Configure SSL certificates for both distributions using AWS Certificate Manager.

Pros:

Cons:

2. Amazon Route 53 with Alias Records

Implementation:

  1. Create a hosted zone for your domain in Route 53.
  2. Create an A record for your root domain, pointing to your website's endpoint (e.g., EC2 instance or load balancer).
  3. Create another A record for the www subdomain, setting it as an Alias to the root domain record.

Pros:

Cons:

3. Application Load Balancer (ALB)

Implementation:

Example rule:

IF (host-header == 'www.example.com') THEN redirect (https://example.com/#{path}?#{query})

Pros:

Cons:

4. AWS Lambda@Edge with CloudFront

Implementation:

  1. Create a CloudFront distribution for your website.
  2. Write a Lambda function to check the host header and redirect if necessary.
  3. Configure the Lambda function to trigger on viewer request events.

Example Lambda function (Node.js):

exports.handler = async (event) => { const request = event.Records[0].cf.request; const headers = request.headers; const host = headers.host[0].value; if (host.startsWith('www.')) { const newHost = host.slice(4); return { status: '301', statusDescription: 'Moved Permanently', headers: { 'location': [{ key: 'Location', value: `https://{newHost}{request.uri}` }] } }; } return request; };

Pros:

Cons:

5. CloudFront Functions

Implementation:

  1. Set up a CloudFront distribution for your website.
  2. Create a CloudFront Function with the following code:

function handler(event) { var request = event.request; var headers = request.headers; var host = headers.host.value; if (host.startsWith('www.')) { var newHost = host.slice(4); var response = { statusCode: 301, statusDescription: 'Moved Permanently', headers: { 'location': { value: https://{newHost}{request.uri} } } }; return response; } return request; }

Associate the function with your CloudFront distribution's Viewer request event.

Pros:

Cons:

Conclusion

Each method has its strengths and is suited to different scenarios:

When choosing the best approach, consider your specific needs, traffic patterns, performance requirements, and management capabilities.