An issue has been frustrating me today in setting up a URL Rewrite in an old .Net Webforms website (EDIT: 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 /.
I've tried setting up in code through global.asax:
if (Request.Url.PathAndQuery.ToLower().Contains("/default.aspx"))
Response.RedirectPermanent("/" + Request.Url.Query, true);
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.
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.
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.
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.
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.
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.
来源:https://stackoverflow.com/questions/30629388/url-rewrite-causing-redirect-loop