且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 .htaccess 重定向中保留 HTTP/HTTPS 协议

更新时间:2023-11-27 09:02:52

尝试添加如下条件:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s) 

它检查 HTTPS 是关闭还是打开,您可以使用反向引用来获取s"字符:

Which checks that either HTTPS is off or it's on and you can use a backreference to fetch the "s" character:

RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s) 
RewriteRule ^ http%1://sub.domain.com:2368%{REQUEST_URI} [P,QSA,L]

因此,如果 HTTPS 关闭,%1 为空,协议为 http://.如果 HTTPS 开启,则s"被分组并且 %1 反向引用是一个s",因此协议是 https://.

So if HTTPS is off, %1 is blank and the protocol is http://. If HTTPS is on, then the "s" is grouped and the %1 backreference is an "s", thus the protocol is https://.

然而,这一切都假设端口 2368 可以处理未加密和 SSL/TLS.

However, this is all assuming that port 2368 can handle both unencrypted and SSL/TLS.