简体   繁体   English

关于301从文件夹重定向到web.config中的子文件夹的困惑

[英]confusion regarding 301 redirect from folder to subfolder in web.config

Say I have my api under this url http://www.example.net/api/trythis and I would like to redirect to http://www.example.net/api/v1/trythis , so in future I can have http://www.example.net/api/v2/trythis or http://www.example.net/api/v3/trythis 假设我的api位于此URL http://www.example.net/api/trythis并且我想重定向到http://www.example.net/api/v1/trythis ,所以将来我可以http://www.example.net/api/v2/trythishttp://www.example.net/api/v3/trythis

I am thinking of doing this in web.config: 我正在考虑在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>

However, if someone access url /api/v2/trythis or /ap1/v3/trythis , won't they be redirected to /api/v1/trythis ? 但是,如果有人访问url /api/v2/trythis or /ap1/v3/trythis ,他们不会重定向到/api/v1/trythis吗? So it will defeat the purpose of versioning? 因此,它将打败版本控制的目的吗?

According to your rules, it will cause multiple redirect errors. 根据您的规则,它将导致多个重定向错误。 Since the "api/v1" also matches the "api". 由于“ api / v1”也与“ api”匹配。

To solve this issue, you should set the condition in your url rewrite rule to avoid multiple matching. 要解决此问题,您应该在url重写规则中设置条件,以避免多个匹配项。

Details, you could refer to below rule: 详细信息,您可以参考以下规则:

     <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>

If you want to avoid v2,v3, I suggest you could add multiple condition as above setting. 如果要避免使用v2,v3,建议您按照上述设置添加多个条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM