Should You Keep the ‘www’ Prefix? Pros, Cons, and Server Config Guide
This article examines whether the ‘www’ subdomain is still necessary in 2023, discusses DNS and cookie implications, and provides practical Apache and Nginx rewrite rules for handling www‑to‑non‑www (and vice versa) redirects while considering SSL and SEO impacts.
Background
In the early days of the Internet, accessing a website required the "www" prefix, which stood for World Wide Web and helped standardize URLs under a single domain. By 2017 and beyond, many sites still use the prefix, while others omit it.
Technical Considerations
The "www" prefix is not obsolete; it can distinguish the web service from other hosts such as mail or FTP. Most URLs remain under .com or country‑code TLDs, and the growing number of generic TLDs can cause confusion without the prefix.
When the root (non‑www) domain is used, its DNS A record must point directly to the web server’s IP address. If availability or performance issues arise, updating the A record can take hours, whereas a CNAME record for the "www" subdomain allows immediate changes.
Developers also need to handle cookies and client‑side storage carefully. Login sessions may differ between www and non‑www domains, so shared cookies, sessionStorage, or localStorage should be configured for the parent domain (e.g., .example.com) to work across subdomains.
Some email clients and word processors automatically convert plain text URLs into clickable links, making the "www" prefix useful in those contexts.
Apache Configuration
To redirect non‑www requests to the www version, add the following to the site’s .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]To perform the opposite redirect (www to non‑www):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]Nginx Configuration
If both www and non‑www domains are defined in the same server block, you can use a conditional rewrite:
server {
listen 80;
listen [::]:80;
server_name www.youdomain.com youdomain.com;
root /var/www/www.youdomain.com;
if ($http_host = www.youdomain.com) {
rewrite ^(.*) http://youdomain.com$1 permanent;
}
}For clearer maintenance, separate the redirects into distinct server blocks:
server {
listen 80;
listen [::]:80;
server_name www.youdomain.com;
return 301 $scheme://youdomain.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name youdomain.com;
# main server configuration goes here
}SSL and Best Practices
When serving sites over HTTPS, ensure that the SSL certificate covers both www and non‑www variants; many certificates do, but a wildcard certificate may be required for full compatibility.
Avoid frequent switching between www and non‑www URLs. Use a permanent 301 redirect so browsers and search engines update their indexes, reducing the risk of broken links or user confusion.
Ultimately, choose the form that best fits your branding and technical requirements, and configure your server consistently.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
