How to force ssl and www for a subdomain as an independent domain?
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?
Related videos on Youtube
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, 2022Comments
-
Pasakgroup over 1 year
I have own the
example.tld
.
I want to force SSL and https forroot-domain
and all of itssubdomains
.
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 asblog.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 ofsubdomain
.
What should I do?-
MrWhite almost 6 yearsTo 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 almost 6 years@MrWhite - Yes, I want to always show ssl and www for domain and its subdomains.
-
Pasakgroup almost 6 yearsAnd, I have cleared the cache, but, It doesn't work!!!
-
-
MrWhite almost 6 yearsThe redirect you posted doesn't enforce
www
for the subdomain as you state? -
Pasakgroup almost 6 yearsYou're right, because before this I've define HTTP_HOST as www version. I will change the code to get public usage.
-
Pasakgroup almost 6 yearsWell, I will test your suggestion and report the result here.
-
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 almost 6 yearsShould 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, shoulddifferent rules
for domain and subdomains like as rules that I have suggested be used? -
Pasakgroup almost 6 yearsWell done! Mission completed!!