URL Rewrite causing redirect loop

限于喜欢 提交于 2019-12-06 07:45:57

IIS has a "default" document feature, could it be that the page default.aspx is part of this list and although you are re-routing to "/" which then in turn translates to "default.aspx". Which then gets translated to "/" via the redirect rule causing the loop.

See your ApplicationHost.config file for the global rules. Also see this url, which documents the hierarchy of the rules.

"The evaluation is performed in a parent-to-child order, which means that parent rules are evaluated first and the rules defined on a last child level are evaluated last." - http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

Try an alternative redirect rule:

Create a separate page with a different name "notdefault.aspx, in the root and re-route all the defaults to the newly create page. (copy default.aspx to nodefault.aspx)

Also see this link for more information to control the defaults: https://technet.microsoft.com/en-us/library/cc753615(v=ws.10).aspx

I ended up adding the following to my web.config to resolve this issue:

<defaultDocument>
  <files>
   <clear />
   <add value="default.aspx" />
  </files>
 </defaultDocument>

based on a reddit post. So my overall web.config for this section looks like this:

<system.webServer>
    <rewrite>
      <rules>
        <rule name="RemoveDefaultAspxFromRoot" stopProcessing="true">
          <match url="default.aspx" ignoreCase="true" />
          <action type="Redirect" url="/" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
    <defaultDocument enabled="true">
      <files>
        <clear />
        <add value="default.aspx" />
      </files>
    </defaultDocument>
  </system.webServer>

This works great without adding other files. I'm not necessarily sure I understand why clearing default documents and adding 'default.aspx' works when 'default.aspx' was already set as the default document through IIS, but it resolved my issue. If anyone can help explain why this is, I'd love to learn.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!