[英]confusion regarding 301 redirect from folder to subfolder in web.config
假设我的api位于此URL http://www.example.net/api/trythis
并且我想重定向到http://www.example.net/api/v1/trythis
,所以将来我可以http://www.example.net/api/v2/trythis
或http://www.example.net/api/v3/trythis
我正在考虑在web.config中执行此操作:
<system.webServer>
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="/api/*" />
<action type="Redirect" url="/api/v1/*" />
</rule>
</rules>
</rewrite>
</system.webServer>
但是,如果有人访问url /api/v2/trythis or /ap1/v3/trythis
,他们不会重定向到/api/v1/trythis
吗? 因此,它将打败版本控制的目的吗?
根据您的规则,它将导致多个重定向错误。 由于“ api / v1”也与“ api”匹配。
要解决此问题,您应该在url重写规则中设置条件,以避免多个匹配项。
详细信息,您可以参考以下规则:
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="api/(.*)" />
<action type="Redirect" url="http://yousitedomain.com/api/v1/{R:1}" />
<conditions>
<add input="{PATH_INFO}" pattern="api/v1/(.*)" negate="true"/>
</conditions>
</rule>
如果要避免使用v2,v3,建议您按照上述设置添加多个条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.