Add www. to apex hostname and subdomains

Detect requests that don't include a www. prefix, and redirect to the equivalent path on a hostname that starts with www., usually to make sure there's only one canonical location for your content.


Use this solution in your VCL service (click RUN below to test this solution or clone it to make changes):


Use this solution in your Compute@Edge service:

  1. Rust
let host_header = header_val(req.get_header(HOST));
if !host_header.starts_with("www.") {
let mut new_location = req.get_url().clone();
new_location.set_host(Some(&format!("www.{}", host_header)))?;
let resp = Response::from_status(StatusCode::PERMANENT_REDIRECT)
.with_header(CACHE_CONTROL, "max-age=86400")
.with_header(LOCATION, new_location);
return Ok(resp);