这两个代码段之间有什么区别?

时间:2020-09-19 15:00:09

标签: amazon-web-services redirect mod-rewrite https amazon-cloudfront

两个片段都将http重定向到https,但是其中一个片段适用于AWS Cloudfront分发,而另一个则不适用。单独使用,两者都可以正常工作。这两个代码段有什么区别?

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^(localhost|127.0.0.1)
    RewriteRule ^/(.*) https://example.com/$1 [R,L] 
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

1 个答案:

答案 0 :(得分:1)

嗯,两个之间有两个区别。

第一个块将重定向到https://example.com/ *

如果HTTPS不是ON 如果HTTP_HOST既不是localhost也不是127.0.0.1

第二个块将使用https协议重定向到与请求中使用的主机相同的主机。

例如:

发件人:http://www.example.com/test?1=2&3=4

收件人:https://www.example.com/test?1=2&3=4

如果HTTPS不是ONHTTP_HOSTwww.example.com(区分大小写)。

由于它将重定向到与请求中的主机相同的主机,因此很可能会创建一个无限循环。关键区别在于您在条件中指定的[OR]标志。