I have own the example.tld .
I want to force SSL and https for root-domain and all of its subdomains.
I have used below code in .htaccess file of root-domain:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.tld$ [NC]
RewriteRule ^(.*)$ https://www.example.tld/$1 [L,R=301]

It looks work perfectly for root-domain.
But, for subdomains like as blog.example.tld , I have used different suggestions, such as:

RewriteCond %{HTTP_HOST} ^blog\.example.\tld$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

But, none of them works properly, and they will redirect to ssl www version of root-domain, instead of ssl www version of subdomain.
What should I do?

  • To clarify, you want to always redirect to www, even for subdomains? In that case, the suggestion you have above for the “blog” subdomain should do as you require, providing you’ve put it in the correct place (although it only works for that one subdomain and you’ve incorrectly escaped the regex - but that’s not the cause of your problem) - if it’s not working then maybe you are seeing a cached response? (Easier to test with 302s) – MrWhite Jul 8 at 16:18
  • @MrWhite - Yes, I want to always show ssl and www for domain and its subdomains. – Pasakgroup Jul 8 at 16:26
  • And, I have cleared the cache, but, It doesn't work!!! – Pasakgroup Jul 8 at 16:31
up vote 3 down vote accepted

Finally, I have found the problem.
Problem had caused by host settings.

Due to some issues, I had to define subdomain as an independent domain in the host. This raised issues with Redirects. Because, instead of subdomain, the main domain was responding to Redirection commands.

To solve this problem, I've transferred subdomain to another host, so Redirection commands for subdomain was replied from new host containing subdomain.

In this new situation, the following simple command forces ssl and www for subdomain:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.sub.example\.tld$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I hope this experience will be useful for others.

  • The redirect you posted doesn't enforce www for the subdomain as you state? – MrWhite Jul 8 at 23:52
  • You're right, because before this I've define HTTP_HOST as www version. I will change the code to get public usage. – Pasakgroup Jul 9 at 0:02
  • "I've transferred subdomain to another host" - Yes, the directives you've posted would be OK if using a different host (or the subdomain points to a different area of the filesystem and you are able to use a different .htaccess file). However, they would result in a conflict if your subdomains all pointed to the same area of the filesystem as the domain apex - which I believe was the initial problem in your question? – MrWhite Jul 9 at 7:34

But, none of them works properly, and they will redirect to ssl www version of root-domain, instead of ssl www version of subdomain.

(I'm assuming all your subdomains map to the same area of the filesystem.)

It sounds like you may have put the directives in the wrong order. If you placed the subdomain redirect after the root-domain redirect then it will never get processed and the root-domain redirect will catch all subdomain requests, and redirect the subdomain back to the root-domain, since the RewriteCond directive is checking whether the requested domain is not www.example.tld - this will also match the subdomain.

However, if you want to always enforce the www subdomain (even for your subdomains) then your directive(s) can be "simplified" into a single rule. For example:

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.?$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

This matches any request that is either HTTP or does not start www. and redirects to HTTPS, prefixing the www subdomain (if necessary). The 3rd RewriteCond directive is necessary to capture the requested hostname, less any www. prefix (if any). The %1 in the substitution is a backreference to the hostname, less any www. prefix.

This naturally affects all domains on the account (if you have more than one).

UPDATE: I've just seen your answer, so I'm not sure whether this will be relevant now or not?

  • Hey! You tried. Cheers!! – closetnoc Jul 9 at 0:17
  • Well, I will test your suggestion and report the result here. – Pasakgroup Jul 9 at 0:24
  • Should I use your suggested rule in .htaccess file of the root-domain? This rule only works when the subdomain and root-domain refer to the same filesystem? For separate filesystems, should different rules for domain and subdomains like as rules that I have suggested be used? – Pasakgroup Jul 9 at 10:32
  • Yes, this code would go near the top of the .htaccess file in the document root of the "root-domain". This rule doesn't only work "when the subdomain and root-domain refer to the same filesystem" as you could use/duplicate the same code for the subdomains if they point elsewhere. If the subdomains point to different filesystems/hosts then you'll need to duplicate your code. But if the subdomains point to different places then it would generally be recommended to be more specific and target the subdomain only - as you have done in your answer. – MrWhite Jul 9 at 10:57
  • 1
    Well done! Mission completed!! – Pasakgroup Jul 9 at 11:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.