WWW to non-WWW on an ALB

Intermediate
⏱️ 10 min read
📚 Updated: Aug 2025
ALB / Route 53 / TLS

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 www at the load balancer (alias A/AAAA or a www CNAME/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.

  1. Open the EC2 console, select your load balancer, and open the Listeners and rules tab.
  2. Select the HTTP : 80 listener and choose Manage rules / Add rule (wording varies slightly in the console).

    ALB HTTP port 80 listener in Listeners and rules
    Amazon EC2
  3. Choose Add rule.

    Add rule button on ALB listener
    Amazon EC2
  4. Configure the rule:

    1. Name: For example www-to-apex-http.
    2. Add conditionHost header → enter www.example.com (your www hostname only).
    ALB listener rule host header condition for www
    Amazon EC2
  5. 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.com without www. Use status 301 (permanent) when you are confident in the cutover.

    ALB redirect action to custom apex host
    Amazon EC2
  6. Set Priority to a number higher than more specific rules (for example 1000) so default forwarding still works for other hosts.
  7. Save or Create the rule.
  8. Confirm the new rule appears under the HTTP listener.

    ALB listener rules list showing www redirect on HTTP
    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.

  1. Open the HTTPS : 443 listener.
  2. In Listeners and rules, select the HTTPS : 443 row (same page as HTTP; only the listener protocol differs).

    ALB HTTPS port 443 listener
    Amazon EC2
  3. Add a rule named similarly (for example www-to-apex-https), then Add conditionHost headerwww.example.com only.
  4. HTTPS listener rule host header www only
    Amazon EC2
  5. Use the same Redirect to URL action to send traffic to example.com on HTTPS (adjust port and protocol in the redirect form if you terminate TLS only on 443).
  6. Under Actions, add Redirect to URL with the same apex host and scheme as on HTTP (for example https and host example.com).

    ALB redirect action for HTTPS listener
    Amazon EC2
  7. Assign priority (for example 1000) and create the rule.
  8. Confirm the new rule appears under the HTTPS listener next to your default forward action.

    ALB HTTPS listener rules showing www redirect
    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 www traffic reaches the ALB and your new rule can run.
Route 53 hosted zone records for apex and www toward ALB
Route 53

Testing

  1. In a browser or with curl -I, request http://www.example.com and https://www.example.com.
  2. Confirm 301 or 302 responses and a Location header pointing at https://example.com/... (or your chosen scheme).
  3. After redirects, the browser should land on your apex hostname in the address bar.

    Browser address bar showing apex domain after redirect
    Browser

Key takeaways

1

Match www with a host header condition; forward or redirect the apex separately.

2

Mirror the redirect on :80 and :443 so both schemes behave consistently.

3

DNS targets the ALB; target groups stay attached to the balancer, not to public DNS records.

Frequently asked questions

Use 301 when the canonical hostname is permanent so search engines consolidate signals. Use 302 during short experiments where you might revert.
Swap the condition and redirect targets: match the apex host header and redirect to www.example.com. DNS must still send both names to the load balancer.
You can enforce redirects at CloudFront instead or in addition. If CloudFront sits in front of the ALB, you still need consistent host behaviour between viewer requests, cache keys, and origin requests.

Next: CloudFront

With a single canonical hostname, you can put CloudFront in front of the same ALB origin for caching and edge TLS.

CloudFront →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful