.htaccess中的HTTPS重定向导致无限重定向循环



我正在尝试使用.htaccess:中的以下内容将所有HTTP URL重定向到HTTPS

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

然而,它只会导致一个无限的重定向循环。有一个有效的SSL证书,网站是用asp.net编写的,以备不时之需。谷歌chrome自动重定向到https,但firefox和edge没有,所以我需要添加这个规则。我能用https加载网站的唯一方法是在地址中显式键入https://。可能的原因是什么?

IIS不支持.htaccess文件。所以我建议您使用IIS URL重写模块,并使用该模块添加规则。

为此,您需要使用web平台安装程序或从下面的链接安装URL重写模块。

https://www.iis.net/downloads/microsoft/url-rewrite

在配置文件中添加以下代码:

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

有关更多详细信息,您可以参考以下链接:

https://stackoverflow.com/a/55531655/11147346

RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://yourwebsite.com/$1 [R,L]

试试

或者使用cloudflare缓存和执行您的项目https://support.cloudflare.com/hc/en-us/articles/115000219871-Troubleshooting-redirect-loop-errorshttps://community.cloudflare.com/t/cloudflare-flexible-ssl-is-it-worth-it/28043https://developers.cloudflare.com/ssl/origin/ssl-modes/

最新更新