在.htaccess重定向preserve HTTP / HTTPS协议重定向、协议、htaccess、preserve

2023-09-02 09:49:30 作者:青春别挥霍

我要重定向端口80到2368 htaccess的,但我想保持请求的协议不变,以便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.