WWW to non-WWW on an ALB

Intermediate
10 min read
Updated: Nov 2026
5 examples
ALB / Route 53 / TLS

Definition and Usage

Many teams pick either apex (example.com) or www (www.example.com as the canonical public hostname. When the canonical name is the apex, requests that arrive with a www host header should get an HTTP redirect so browsers and search engines converge on one URL.

What does a www-to-apex ALB rule do?

An Application Load Balancer listener rule matches a host header of www.example.com and returns a redirect to example.com (same path and query). The apex continues to hit the default forward action to your target group.

When should you configure this?

After DNS sends both apex and www to the ALB (see Route 53), and after HTTP to HTTPS plus ACM so :80 and :443 behave predictably.

What you will configure

Host-header conditions on HTTP and HTTPS listeners, redirect actions to the apex host, rule priorities that leave room for more specific rules, and matching DNS aliases for both names.

CDN and the reverse cutover

You can enforce host redirects at CloudFront instead or in addition. If you prefer www as canonical, use an apex to www redirect with the opposite condition and Location host.

Match only www.

The redirect rule’s host condition should be www.example.com, not the apex. Matching the apex on a rule that redirects to the apex is a common cause of redirect loops.

ERR_TOO_MANY_REDIRECTS.

Do not add the apex hostname as a second condition on the same redirect rule. Keep apex traffic on the default forward (or a separate rule that does not bounce).

Priority numbers.

Lower numbers evaluate earlier on an ALB. Use a higher priority (for example 1000) for this catch-all www redirect so path or host rules for other apps can still win first.

Core Concepts

Terms you will use while building the redirect:

ConceptMeaning
ApexBare domain such as example.com (no www)
www hostHostname clients type as www.example.com
Host header conditionListener rule match on the HTTP Host value
Redirect actionALB responds with 301/302 and a Location URL
Rule priorityLower number evaluated first; default rule is last
301 / 302Permanent vs temporary redirect status for clients and SEO
Alias A / AAAARoute 53 records that point apex (and often www) at the ALB
Canonical hostnameThe single public name you want bookmarks and search to use

Prerequisites

Have these ready before you add listener rules:

  • An AWS account with access to EC2 load balancing and Route 53.
  • An Application Load Balancer serving your site, with DNS pointing both apex and www at that ALB (see Route 53).
  • HTTPS ready if you follow the port 443 section: ACM certificate covering apex and www, plus HTTP to HTTPS as needed.
ALB HTTP port 80 listener in Listeners and rules
Amazon EC2

Examples

Add a www host rule on HTTP, configure the redirect, mirror it on HTTPS, then confirm DNS and browser behaviour.

Example 1 — Open HTTP :80 and Add a Rule

Start on the HTTP listener so plain http://www... clients can redirect.

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

How It Works

  1. Listener rules evaluate before the default forward action.
  2. HTTP :80 still matters for clients that type http:// or follow old links.
  3. Add rule creates a conditional action; the default rule stays for the apex.
  4. Pair with HTTPS later so both schemes behave the same for www.

Example 2 — Host Header Condition for www Only

Name the rule and match only the www hostname.

  1. Set a Name such as www-to-apex-http.
  2. Choose Add condition → Host header.
  3. Enter www.example.com (your real www hostname)—not the apex alone.
ALB listener rule host header condition for www
Amazon EC2

How It Works

  1. Host header is what the browser sent as the intended site name.
  2. www only keeps apex traffic on the default forward (or other rules).
  3. Matching apex here on a redirect-to-apex rule causes loops.
  4. Multiple alternate domains need separate conditions or rules per host.

Example 3 — Redirect Action, Priority, and Save

Send matching requests to the apex host with a 301 when the cutover is permanent.

  1. Under Actions, add Redirect to URL.
  2. Choose URI parts (or equivalent), enable custom host/path/query if shown, and set Host to example.com without www.
  3. Use status 301 when you are confident; use 302 for short experiments.
  4. Set Priority higher than more specific rules (for example 1000), then create the rule and confirm it appears under HTTP.
ALB redirect action to custom apex host
Amazon EC2
ALB listener rules list showing www redirect on HTTP
Amazon EC2

How It Works

  1. Redirect returns Location without forwarding to targets for that request.
  2. Preserve path and query so deep links keep working after the host change.
  3. 301 consolidates SEO signals; 302 is easier to roll back.
  4. Higher priority numbers leave room for path rules that must win first.

Example 4 — Mirror the Rule on HTTPS :443

Repeat the same host match and apex redirect on the TLS listener.

  1. Open the HTTPS : 443 listener on the same load balancer.
  2. Add a rule (for example www-to-apex-https) with Host header = www.example.com only—do not add the apex as a second condition.
  3. Add Redirect to URL to https://example.com (adjust scheme/port in the form if needed).
  4. Assign priority (for example 1000), create the rule, and confirm it appears next to the default forward.
ALB HTTPS port 443 listener
Amazon EC2
HTTPS listener rule host header www only
Amazon EC2
ALB HTTPS listener rules showing www redirect
Amazon EC2

How It Works

  1. TLS clients hit 443 directly; they never see the HTTP rule alone.
  2. Same host condition keeps behaviour consistent across schemes.
  3. Certificate must cover www or the handshake fails before the redirect.
  4. After hosts are stable, consider HSTS so browsers prefer HTTPS on return visits.

Example 5 — Route 53 Records and Browser Test

DNS must deliver www to the ALB; then verify Location headers and the final address bar.

  1. In Route 53, keep an alias A (and AAAA if needed) for the apex pointing at the load balancer DNS name—not a target group ARN.
  2. Keep a www record (CNAME or alias) to the same load balancer so the new rule can run.
  3. Request http://www.example.com and https://www.example.com (browser or curl -I).
  4. Confirm 301/302 and a Location header for the apex; the address bar should end on the non-www hostname. Optional: enforce the same at CloudFront host redirects if the CDN is in front.
Route 53 hosted zone records for apex and www toward ALB
Route 53
Browser address bar showing apex domain after redirect
Browser

How It Works

  1. Redirects run after DNS—www must resolve to the ALB first.
  2. Target groups stay on the ALB; public DNS never points at a target group ARN.
  3. curl -I shows status and Location without following the full page load.
  4. SEO prefers one hostname; pick apex or www and stick to it (see also canonical URL tags).

Edge Cases & Pitfalls

Most failures are redirect loops, missing www DNS, or HTTPS-only rules.

loop

ERR_TOO_MANY_REDIRECTS

The redirect rule also matches the apex, or HTTPS redirects back to HTTP. Match www only; keep apex on forward.

dns

www never hits the ALB

Fix the www alias/CNAME so it resolves to the same load balancer as the apex.

tls

Certificate does not cover www

Browsers fail the handshake before your HTTPS redirect rule runs. Include www (or a wildcard) in ACM.

http

Works on HTTPS only

Add the matching rule on port 80, or clients on plain HTTP keep serving www without redirect.

cdn

CloudFront cache of old host

If a CDN sits in front, invalidate or align viewer-protocol and host behaviour so cached www responses do not fight the ALB.

priority

Wrong rule wins

A more specific host/path rule with a lower priority number may never reach your www redirect—or the reverse. Check evaluation order.

Key Takeaways

  • Match www with a host header condition; forward the apex separately.
  • Mirror on :80 and :443 so both schemes behave consistently.
  • DNS targets the ALB; target groups stay attached to the balancer.
  • Next: optional CloudFront in front once the canonical hostname is stable.

One line: DNS both names to ALB → host=www redirect to apex on 80 and 443 → test Location.

Frequently Asked Questions

The redirect rule should fire when the client intended the www hostname. If you also match the apex hostname on the same rule that redirects to the apex, healthy requests can bounce until the browser reports ERR_TOO_MANY_REDIRECTS.
Route 53 alias records for a website usually target the load balancer DNS name or a CloudFront distribution, not a target group ARN. Target groups are attached to the load balancer; DNS still resolves to the load balancer endpoint.
Yes, if you serve both HTTP and HTTPS and want www visitors upgraded consistently. The HTTPS rule should still match only the www host when redirecting to the apex, while your default forward action serves the apex and other allowed hosts.
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, put CloudFront in front of the same ALB origin for caching and edge TLS.

CloudFront →

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