我如何在只有一个non-www equivalent with trailing slash单301重定向(避免重定向链)的情况下,将URL重定向到?我使用的是ASP.net 4.5 / C# / Web和在RouteConfig.cs中注册的路由。
一种选择是检查每个页面后面的代码中的URL并重新构建URL,但我更喜欢让IIS使用重写规则来处理它。
从这个映像中可以看到(通过chrome,客户端),有两个301重定向,因为在我的web.config中,我有两个规则,一个用于更改小写URL,另一个用于添加尾斜杠。

也许IIS中有一个选项可以防止重定向,直到所有URL重写在内部运行为止。我找过了,但还没找到解决办法。
发布于 2015-07-13 11:54:25
在web.config中(您需要安装IIS重写模块)
<system.webServer>
<rewrite>
<rules>
<rule name="noslash" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".+[^\/]$" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="www.yourdomain.com" />
<add input="{HTTP_HOST}" pattern="yourdomain.com" />
</conditions>
<action type="Redirect" url="http://yourdomain.com/{R:0}/" />
</rule>
<rule name="www" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".+\/$" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="www.yourdomain.com" />
</conditions>
<action type="Redirect" url="http://yourdomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>EDIT2:如果url不存在,则在url末尾添加尾斜杠
https://stackoverflow.com/questions/31381962
复制相似问题