Redirect Domain to HTTPS & WWW, but Subdomain Non-WWW

domain to https, www but subdomain non-www

When it comes to a website with subdomains, it’s often a good idea to redirect the main domain to HTTPS and WWW while keeping subdomains non-WWW. There are numerous posts on the web about domain and subdomain redirection in terms of HTTPS, WWW, and non-WWW. But why do even the most seemingly perfect solutions sometimes fail?

Redirecting the main domain to HTTPS & WWW is a relatively straightforward task with many available methods. However, redirecting a subdomain to non-WWW requires a bit more effort.

First and foremost, you need to install an SSL certificate. You can choose to purchase one or generate a free one from Let’s Encrypt. If you’re using Blogger, you can even get it for free without involving third-party providers.

There are clear and precise rules for domain and subdomain redirection. When it comes to HTTPS, WWW, and non-WWW, there’s a simple solution if you follow these rules both before and after implementing the redirection in your .htaccess or cPanel.

Let’s say you want your main domain to appear like this:

https://www.example.com

… and a subdomain like this:

https://sub.example.com

It’s important to remember that WWW is technically a subdomain. Planning your domain and subdomain structure as shown above might be a smart move.

Now, let’s dive into the redirection. We’ll introduce two sets of redirection rules.

But one crucial thing to remember: Before testing the redirection, don’t forget to clear any issues caused by caching.

How to Redirect the Main Domain to HTTPS and WWW using .htaccess File?

First, make sure you’ve correctly added the “www” CNAME in your domain’s DNS manager.

Insert the following code into the .htaccess file in the main domain’s directory:

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

Redirecting the main domain is typically easier to implement than redirecting a subdomain. You can also configure this in your Domain DNS Manager or via cPanel.

How to Redirect a Subdomain to HTTPS Non-WWW using .htaccess?

Similar to the main domain, ensure you’ve added a “www” CNAME for the subdomain correctly in your domain’s DNS manager. You can set up a CNAME like this: “sub” and then “www.sub”. In the past, it may not have seemed logical to add “www.sub” for a subdomain, but my perspective changed after testing.

Insert the following code into the .htaccess file in the subdomain’s directory:

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

Making These Redirections Work

First, if you’re using WordPress, refresh your permalinks.

  • Go to settings and click on Permalinks.
  • Change the permalink structure.
  • Click Save.
  • Revert your structure to the previous setting and click Save again.

Clear the cache thoroughly, not just in your browser but also in any caching plugins, on the server, through a CDN, and so on. The path from WordPress to the visitor’s browser should be clean.

Related posts