WWW to non-WWW on an ALB

What you’ll learn
Many teams pick either apex (example.com) or www (www.example.com) as the canonical public hostname. When the canonical name is the apex, you want requests that arrive with a www host header to answer with an HTTP redirect so browsers and search engines converge on one URL.
This guide uses an Application Load Balancer listener rule with a host header condition and a redirect action. Complete HTTP to HTTPS and ACM setup first so both :80 and :443 listeners behave predictably.
Prerequisites
- An AWS account with access to EC2 load balancing and Route 53.
- An Application Load Balancer serving your site, with DNS (for example from Route 53) pointing both apex and
wwwat the load balancer (alias A/AAAA or awwwCNAME/alias to the same ALB). - Certificates and listeners for HTTPS if you follow the port 443 section.
Listener rule on HTTP (port 80)
Add a rule on the HTTP listener so plain http://www.example.com/... clients receive a redirect to the apex.
- Open the EC2 console, select your load balancer, and open the Listeners and rules tab.
Select the HTTP : 80 listener and choose Manage rules / Add rule (wording varies slightly in the console).

Amazon EC2 Choose Add rule.

Amazon EC2 Configure the rule:
- Name: For example
www-to-apex-http. - Add condition → Host header → enter
www.example.com(your www hostname only).

Amazon EC2 - Name: For example
Actions: Add Redirect to URL. Choose URI parts (or equivalent), enable Custom host, path, query if shown, and set Host to your apex name
example.comwithoutwww. Use status 301 (permanent) when you are confident in the cutover.
Amazon EC2 - Set Priority to a number higher than more specific rules (for example
1000) so default forwarding still works for other hosts. - Save or Create the rule.
Confirm the new rule appears under the HTTP listener.

Amazon EC2
Listener rule on HTTPS (port 443)
Repeat the pattern on the HTTPS listener so https://www.example.com also redirects to the apex.
- Open the HTTPS : 443 listener.
In Listeners and rules, select the HTTPS : 443 row (same page as HTTP; only the listener protocol differs).

Amazon EC2 - Add a rule named similarly (for example
www-to-apex-https), then Add condition → Host header →www.example.comonly. 
Amazon EC2 - Use the same Redirect to URL action to send traffic to
example.comon HTTPS (adjust port and protocol in the redirect form if you terminate TLS only on 443). Under Actions, add Redirect to URL with the same apex host and scheme as on HTTP (for example
httpsand hostexample.com).
Amazon EC2 - Assign priority (for example
1000) and create the rule. Confirm the new rule appears under the HTTPS listener next to your default forward action.

Amazon EC2
Route 53 records (reminder)
Redirects happen after DNS resolves the client to your load balancer. In Route 53 you typically keep:
- An alias A (and AAAA if you use IPv6) for the apex pointing at the load balancer DNS name, not a target group ARN.
- A www record (CNAME or alias) that also resolves to the same load balancer so
wwwtraffic reaches the ALB and your new rule can run.

Testing
- In a browser or with
curl -I, requesthttp://www.example.comandhttps://www.example.com. - Confirm
301or302responses and aLocationheader pointing athttps://example.com/...(or your chosen scheme). After redirects, the browser should land on your apex hostname in the address bar.

Browser
Key takeaways
Match www with a host header condition; forward or redirect the apex separately.
Mirror the redirect on :80 and :443 so both schemes behave consistently.
DNS targets the ALB; target groups stay attached to the balancer, not to public DNS records.
Frequently asked questions
www.example.com. DNS must still send both names to the load balancer.Next: CloudFront
With a single canonical hostname, you can put CloudFront in front of the same ALB origin for caching and edge TLS.
Use a higher numeric priority (for example 1000) for catch-all style redirects so more specific rules (paths, other hosts) can still evaluate first. Lower numbers run earlier on an Application Load Balancer.
8 people found this page helpful
