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 thewwwsubdomain.redir https://example.com{uri}: This directive tells Caddy to redirect any request tohttps://www.example.comtohttps://example.com, preserving the original URI path.- For example,
https://www.example.com/aboutwould redirect tohttps://example.com/about.
- For example,
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
wwwto 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
wwwand non-wwwto 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:
@orexample.com(depending on your DNS provider’s interface) - Value/Points to:
Your_Server_IP_Address
- Type:
- CNAME Record for
www: Forwww.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 toexample.com.)
- Type:
Why use CNAME for www?
- Flexibility: If your server’s IP address ever changes, you only need to update the A record for
example.com. - Standard Practice: This is a very common and recommended DNS setup for websites.
In summary, your DNS records would look something like this:
| Type | Name/Host | Value/Points to |
|---|---|---|
A | @ | Your_Server_IP_Address |
CNAME | www | example.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.




