web.config redirect to https causing redirect loop

时间:2016-03-02 11:02:22

标签: asp.net redirect web-config http-status-code-301

I am trying to use this code in the web.config to redirect all pages on the website to https:// however as soon as I try this I get an infinite loop (Firefox tells me the redirect will never end properly), but I have no idea why. I only have access to the FTP and can't directly access the server so I can't use the GUI method. I'm in unfamiliar territory with this and any help would be greatly appreciated.

<rule name="Redirect to https" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>

1 个答案:

答案 0 :(得分:1)

尝试使用此规则,而不是检查HTTPs == off,而是检查HTTPS != ON

<rule name="Redirect to https" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" pattern="^ON$" negate="true" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>

我无法解释为什么你的规则不起作用,但这是我一直在使用的规则,而且我没有问题。值得注意的是:在使用HTTP_HOST值时,我偶尔会看到IIS重写规则的问题,几乎就像它有错误的值一样,因此可以安全地说IIS重写规则不是100%可靠。

相关问题