问题原文
原帖链接 · Pasakgroup · CC BY-SA 4.0
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?
已采纳解答
原帖链接 · Pasakgroup · CC BY-SA 4.0
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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
