Sandbox99 Chronicles

Beyond the Basics: Advanced Caddy www Domain Configuration

caddy non-www

Written by Jose Mendez

Hi, I’m Jose Mendez, the creator of sandbox99.cc. with a passion for technology and a hands-on approach to learning, I’ve spent more than fifteen years navigating the ever-evolving world of IT.

Published Jun 23, 2025 | Last updated on Jun 23, 2025 at 11:58PM

Reading Time: 4 minutes

Seamlessly handling www and non-www domains

In the vast landscape of web hosting, setting up your server correctly is paramount for both user experience and search engine optimization (SEO). One of the most common configuration puzzles developers and site owners face is how to effectively manage both the “www” (e.g., www.example.com) and non-“www” (e.g., example.com) versions of their domain. While seemingly a minor detail, getting this right can significantly impact your site’s discoverability and performance.

Caddy, the powerful, open-source web server, has revolutionized server management with its simplicity and automation, especially when it comes to SSL certificates. However, even with Caddy’s user-friendly approach, questions often arise regarding best practices for handling domain variations. In this blog post, we’ll dive into the specifics of configuring your Caddy server to gracefully manage www and non-www domains, focusing on the most recommended strategy for optimal SEO and a streamlined setup. We’ll explore common questions and provide clear, actionable answers to ensure your website is serving content efficiently and intelligently.

How do we configure our domain to use “www”?

here are a couple of ways to handle www in Caddyfile, and the best approach depends on whether you want www.example.com to be the primary, or if you prefer it to redirect to example.com.

Option 1: Redirect www.example.com to example.com


This is the most common and generally recommended approach for SEO and simplicity. You add a separate site block for www.example.com and use Caddy’s redir directive to redirect it.

https://example.com {
    root * /srv
    file_server
}

https://www.example.com {
    redir https://sample.com{uri}
}

Explanation:

  • https://www.example.com { ... }: This block defines the configuration for the www subdomain.
  • redir https://example.com{uri}: This directive tells Caddy to redirect any request to https://www.example.com to https://example.com, preserving the original URI path.
    • For example, https://www.example.com/about would redirect to https://example.com/about.

Below is the example screenshot that I have successfully implemented option 1

Option 2: Serve Both example.com and www.example.com from the Same Block


If you want both example.com and www.example.com to serve the exact same content without a redirect, you can list both domain names in the same site block.

https://example.com, https://www.example.com {
    root * /srv
    file_server
}

Explanation:

  • https://example.com, https://www.example.com { ... }: By separating the domain names with a comma, you’re telling Caddy that this single configuration block applies to both hostnames. Caddy will automatically handle SSL certificates for both.

Which Option to Choose?

  • For SEO and simplicity, Option 1 (redirecting www to non-www) is generally preferred. It consolidates your domain’s authority under a single URL, which can be beneficial for search engine rankings.
  • Option 2 is suitable if you genuinely want both www and non-www to be equally accessible and serve the same content without any redirects. However, be aware that search engines might treat them as separate entities, potentially diluting your SEO efforts.

Update your DNS records

You’ll need to add a CNAME record for your www entry with your DNS host (e.g., Google DNS, GoDaddy, Namecheap, Bluehost, Cloudflare). This ensures www.example.com points to the same server as example.com.

Here’s why and how:

  • A Record for your root domain: Your main domain, example.com (often referred to as the “naked domain” or “apex domain”), will typically have an A record that points directly to the IP address of your Caddy server.
    • Type: A
    • Name/Host: @ or example.com (depending on your DNS provider’s interface)
    • Value/Points to: Your_Server_IP_Address
  • CNAME Record for www: For www.example.com, it’s best practice to create a CNAME record that points to your root domain (example.com).
    • Type: CNAME
    • Name/Host: www
    • Value/Points to: example.com (or sometimes your DNS provider might auto-complete this to example.com.)

Why use CNAME for www?

  1. Flexibility: If your server’s IP address ever changes, you only need to update the A record for example.com.
  2. Standard Practice: This is a very common and recommended DNS setup for websites.

In summary, your DNS records would look something like this:

TypeName/HostValue/Points to
A@Your_Server_IP_Address
CNAMEwwwexample.com

Below is the example screenshot that I have an entry from my sandbox99.cc

Final Thoughts: The Path to a Polished Web Presence

Successfully configuring your Caddy server to handle www and non-www domains, particularly by implementing a consistent redirect strategy, is a small but mighty step towards a robust and SEO-friendly online presence. By opting for Option 1 – redirecting www.example.com to example.com (or vice-versa, depending on your preference for the canonical URL) – you consolidate your domain authority, prevent duplicate content issues, and provide a clear, unified path for search engines to crawl and rank your site.

Caddy’s elegant design, with its automatic HTTP-to-HTTPS redirects and straightforward Caddyfile syntax, significantly simplifies what used to be a complex server administration task. Remember that proper DNS configuration, specifically using a CNAME record for your www subdomain, is the foundational layer that makes these server-side redirects possible.

In an ever-evolving digital world, simplicity and efficiency are key. By following these guidelines, you’re not just setting up a server; you’re building a reliable, performant, and search-engine-optimized foundation for your website, ensuring a smooth experience for all your visitors.

Related Post

Caddy: The Modern Web Server You Need to Know

Caddy: The Modern Web Server You Need to Know

The Shifting Landscape of Web Servers For years, a few titans have dominated the web server landscape. Names like Nginx, Apache2, and Microsoft IIS have been synonymous with hosting websites and applications, powering the vast majority of the internet. Their robust...

read more
Docker & Docker Compose Cheatsheet

Docker & Docker Compose Cheatsheet

Introduction to Docker In the world of software development, the phrase "it works on my machine" used to be a common, frustrating refrain. This is precisely the problem Docker set out to solve. Introduced in 2013, Docker has revolutionized how applications are built,...

read more