简体   繁体   中英

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

I am thinking of doing this in 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 ? 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".

To solve this issue, you should set the condition in your url rewrite rule to avoid multiple matching.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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