How to force ssl and www for a subdomain as an independent domain?

8,912

Solution 1

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.

Solution 2

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?

Share:
8,912

Related videos on Youtube

Pasakgroup
Author by

Pasakgroup

I'm a web developer at the pasakgroup. Our store sale digital and electronic products, like as: computer and server equipment, computer network products, fiber optics, solar panel, wireless antenna, VoIP equipment, CCTV camera, security camera, video surveillance systems, video recording systems (video servers, DVR, NVR), EAS gates (shop anti-lift systems), access control gates, time attendance systems, automatic barrier, automatic door, automatic roller shutter and roll up doors, ups and battery. I'm working on web design and SEO for pasakgroup.com. I love my job and so interested in SEO. I'm also a technical manager in IT and electronic projects. My education has ended at master of science at Information Technology / computer networking at QIAU. I also have CISCO CCNA certificate.

Updated on September 18, 2022

Comments

  • Pasakgroup
    Pasakgroup over 1 year

    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?

    • MrWhite
      MrWhite almost 6 years
      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)
    • Pasakgroup
      Pasakgroup almost 6 years
      @MrWhite - Yes, I want to always show ssl and www for domain and its subdomains.
    • Pasakgroup
      Pasakgroup almost 6 years
      And, I have cleared the cache, but, It doesn't work!!!
  • MrWhite
    MrWhite almost 6 years
    The redirect you posted doesn't enforce www for the subdomain as you state?
  • Pasakgroup
    Pasakgroup almost 6 years
    You're right, because before this I've define HTTP_HOST as www version. I will change the code to get public usage.
  • Pasakgroup
    Pasakgroup almost 6 years
    Well, I will test your suggestion and report the result here.
  • MrWhite
    MrWhite almost 6 years
    "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?
  • Pasakgroup
    Pasakgroup almost 6 years
    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
    Pasakgroup almost 6 years
    Well done! Mission completed!!