Best Practices for Redirecting Main Domains to HTTPS + WWW While Keeping Subdomains HTTPS Non-WWW

For websites with subdomains, it’s often recommended to redirect the main domain to HTTPS and WWW while leaving subdomains without WWW. Many guides explain these redirects, but even experienced users sometimes encounter problems if the planning and implementation aren’t done correctly.

Redirecting the main domain to HTTPS + WWW is relatively straightforward, but redirecting a subdomain to HTTPS non-WWW requires extra care.

Step 1: Install an SSL Certificate

You can purchase an SSL certificate or generate a free one from Let’s Encrypt. Platforms like Blogger even allow free SSL without third-party involvement.

There are clear rules for domain and subdomain redirection. Following them ensures proper handling of HTTPS, WWW, and non-WWW URLs. Planning your domain structure in advance avoids conflicts later.

For example, you might want your URLs to look like this:

  • Main domain: https://www.example.com
  • Subdomain: https://sub.example.com

Remember: WWW is technically a subdomain. Structuring your main domain and subdomains thoughtfully is a key step in smooth redirection.

Redirecting the Main Domain to HTTPS + WWW via .htaccess

Ensure the “www” CNAME is correctly added in your domain’s DNS.

Add the following code to your main domain’s

.htaccess
file:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Redirecting the main domain is usually easier than handling subdomains and can also be configured in your domain DNS manager or cPanel.

Redirecting a Subdomain to HTTPS Non-WWW via .htaccess

For subdomains, make sure DNS CNAME records for both sub and www.sub exist. While www.sub might seem unnecessary, including it can prevent unexpected redirection issues.

Add the following code to the subdomain’s

.htaccess
file:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^www\.sub\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule ^(.*)$ https://sub.example.com/$1 [R=301,L]

Ensuring Redirections Work Properly

If you are using WordPress:

  • Go to Settings → Permalinks.
  • Change the permalink structure and click Save.
  • Revert to the previous structure and Save again.

Clear caches thoroughly, including:

  • Browser cache
  • WordPress caching plugins
  • Server-side cache
  • CDN cache

A clean path from server to visitor’s browser ensures that the redirections function correctly.