我必须在 htaccess 中将端口 80 重定向到 2368,但我想保持请求的协议完整,以便 SSL 不会中断。
我目前有这个:
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteRule ^ http://sub.domain.com:2368%{REQUEST_URI} [P,QSA,L]
其工作正常,但如果可能的话,我希望从 %{HTTP_HOST} 条件中获取协议。
有没有一种方法可以使其更加动态,而无需硬编码域和协议?
它看起来很慢。
Jon 提供的技巧是一个很好的技巧,但我担心如果你想使用更多它可能会崩溃[OR]
条件,如果您使用更多反向引用,您必须小心使用哪个数字(%1
or %2
or ..).
我认为最优雅、最强大、最干净的解决方案智能 HTTP/HTTPS 处理 http://www.askapache.com/htaccess/http-https-rewriterule-redirect.html就是设置一个变量:
# initialization code - put only once at the beginning of .htaccess
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
# simply use %{ENV:proto} in your rules:
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteRule ^ %{ENV:proto}://sub.domain.com:2368%{REQUEST_URI} [P,QSA,L]
优点还在于初始化代码只需运行一次,因此如果您有更多重写规则,那么生成的代码会更短、更优雅。
请注意,常见的设置是 cloudflare 提供的 https,在这种情况下,HTTPS 在服务器本身上未“打开”。在这种情况下,您需要额外的规则,例如(注意顺序):
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"https"' [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)