在 .htaccess 重定向中保留 HTTP/HTTPS 协议 [英] Preserve HTTP/HTTPS protocol in .htaccess redirects

查看:38
本文介绍了在 .htaccess 重定向中保留 HTTP/HTTPS 协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在 htaccess 中将端口 80 重定向到 2368,但我想保持请求的协议完整,以便 SSL 不会中断.

I have to redirect port 80 to 2368 in htaccess but I'd like to keep the requested protocol intact so that SSL doesn't break.

我目前有这个:

RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteRule ^ http://sub.domain.com:2368%{REQUEST_URI} [P,QSA,L]

它工作正常,但如果可能的话,我希望从 %{HTTP_HOST} 条件中获取协议.

which works correctly but I'd like the protocol to be taken from the %{HTTP_HOST} condition if possible.

有没有办法让它在没有硬编码域和协议的情况下更加动态?

Is there a way to get this to be more dynamic without hard coding domains and protocols?

看起来很慢.

推荐答案

尝试添加如下条件:

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.

这篇关于在 .htaccess 重定向中保留 HTTP/HTTPS 协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆