大网数据广告活动
网站运营与故障问答

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

· 52CE 我爱测

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_HOS…

问题原文

原帖链接 · 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-07-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。