URL重写导致重定向循环 [英] URL Rewrite causing redirect loop

查看:155
本文介绍了URL重写导致重定向循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天在一个旧的.Net Webforms网站上设置URL Rewrite时,一个问题令我感到沮丧(编辑:这是一个网站项目,而不是Web应用程序项目,如果这有所不同)。我正在处理的网站链接很多,都链接到webroot.com/default.aspx而不仅仅是webroot.com。我们想要设置(SOMEWHERE,此时我不在乎它是否在global.asax中作为重定向或web.config)从/default.aspx转到/.

An issue has been frustrating me today in setting up a URL Rewrite in an old .Net Webforms website ( It is a Web Site project, not a Web Application project, if that is making a difference). The site I'm working on is linked to a lot, all linking to "webroot.com/default.aspx" rather than just "webroot.com". We're wanting to set up (SOMEWHERE, at this point I don't care if it's in global.asax as a redirect or web.config) a URL rewrite to go from /default.aspx to /.

我尝试通过global.asax设置代码:

I've tried setting up in code through global.asax:

if (Request.Url.PathAndQuery.ToLower().Contains("/default.aspx"))
  Response.RedirectPermanent("/" + Request.Url.Query, true);

这导致了一个重定向循环,我完全理解为什么,尝试它是一个愚蠢的想法。没有其他目录有default.aspx,所以我现在不担心会抓住其他目录。

This leads to a redirect loop, which I understand totally understand why, was a dumb idea to try it. No other directories has a default.aspx, so I'm not worried about catching others at the moment.

我尝试通过web.config设置URL重写在system.webServer节点中,如下所示:

I've tried setting up a URL Rewrite through web.config in the system.webServer node, seen here:

<rewrite>
  <rules>
    <rule name="RemoveDefaultAspxFromRoot" stopProcessing="true">
      <match url="default.aspx" ignoreCase="true" />
      <action type="Rewrite" url="/" appendQueryString="true" />
    </rule>
  </rules>
</rewrite>

这会导致同样的问题,这让我很困惑,因为我认为重写不会执行重定向,但显然确实如此?

This leads to the same issue, which puzzles me as I didn't think a rewrite would perform a redirect, but apparently it does?

我尝试在规则中添加以下内容作为巫毒博客帖子的想法:

I've tried adding in the following in the rule as a voodoo blog-post idea:

<conditions logicalGrouping="MatchAll">
  <add input="{REMOTE_PORT}" pattern=".*" />
</conditions>

没有骰子;仍然陷入重定向循环。

No dice; still getting stuck in a redirect loop.

我甚至手动将URL Rewrite模块安装到IIS并通过gui设置规则,可以看到在这里:

I even went through installing the URL Rewrite module manually to IIS and setting up the rule through there via the gui, which can be seen here:

http://i.imgur。 com / ovYpfhM.png

我仍然遇到重定向问题。

I'm still getting a redirect issue.

任何人都可以看到我遗失的任何东西,还是有其他建议?奇怪的是,这个问题似乎是通过使用Rewrite操作解决给其他人的,但是它对我不起作用。

Can anyone see anything that I'm missing, or have other suggestions? It's strange that this issue seems to be resolved for other people by using the Rewrite action, but that it's not working for me.

我在IIS7上尝试过这些解决方案在本地发布并通过IIS和IIS Express通过VS2013进行设置。我们的生产服务器使用IIS7。

I've tried these solutions on IIS7 by publishing locally and setting it up through IIS, and IIS Express through VS2013. Our production servers use IIS7.

推荐答案

我最终将以下内容添加到我的web.config中以解决此问题:

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

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

基于reddit帖子。因此,我对此部分的整体web.config如下所示:

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>

这不会添加其他文件,效果很好。我不一定确定我理解为什么清除默认文档并添加'default.aspx'时,'default.aspx'已经通过IIS设置为默认文档,但它解决了我的问题。如果有人能帮助解释为什么会这样,我很乐意学习。

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.

这篇关于URL重写导致重定向循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆