简体   繁体   English

强制对主域而不是子域使用HTTPS

[英]Forcing HTTPS for Main Domain, Not Sub-Domain

I have my site and several sub-domains on a c-Panel server and would like to achieve the following functionality: 我在c-Panel服务器上有我的站点和几个子域,并希望实现以下功能:

Force HTTPS and WWW for all traffic to example.com for all traffic to my main site -> https://www.example.com/what-ever-page-or-directory 强制对所有流量的HTTPS和WWW到对我主站点的所有流量的example.com-> https://www.example.com/what-ever-page-or-directory

I also have a sub.example.com as sub-domain setup through c-Panel (not re-directed), whose document root is /public_html/sub and this sub-domain doesn't have an SSL. 我也有一个sub.example.com作为通过c-Panel进行子域设置(未重定向 ),其文档根目录为/ public_html / sub,并且该子域没有SSL。 I tried putting in the following HTACCESS code: 我尝试输入以下HTACCESS代码:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

which works for redirecting all traffic on the main site, BUT when I try to access my sub-domain, http://sub.example.com , I get a redirect error saying This webpage has a redirect loop . 当我尝试访问子域http://sub.example.com时 ,该方法可用于重定向主站点BUT上的所有流量,但出现重定向错误,提示此网页具有重定向循环

Thanks in advance for any help! 在此先感谢您的帮助! I found similar posts but nothing dealing with my specific question. 我发现了类似的帖子,但没有解决我的具体问题。 I'm not great with HTACCESS directives and the redirect error is throwing me for a loop. 我对HTACCESS指令不满意,并且重定向错误使我陷入循环。 Thanks again! 再次感谢!

Try : 尝试:

RewriteEngine On

#Exclude subdomain
RewriteCond %{HTTP_HOST} !^sub.domain.com$
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

To exclude multiple subdomains you can use multiple RewriteConds : 要排除多个子域,可以使用多个RewriteConds:

RewriteCond %{HTTP_HOST} !^foo.domain.com$
RewriteCond %{HTTP_HOST} !^bar.domain.com$

or just 1 liner pattern : 或只是一种班轮模式:

 RewriteCond %{HTTP_HOST} !^(foo|bar).domain.com$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM