【问题标题】:IIS Api redirect ruleIIS API 重定向规则
【发布时间】:2013-02-06 23:25:39
【问题描述】:

我需要一些关于 web.config 中重定向正则表达式的帮助。我有一个网站 UI 和一个应用程序 UI,它们访问相同的服务器端 API,但我正在解耦从其中任何一个调用的 url,以使应用程序免受 API 更新的影响。

这是我到目前为止在我的 web.config 中的内容,它适用于将 api.mywebsite.com 重定向到 mywebsite.com,但是我如何编写正则表达式以在末尾添加版本以便 http://api.mywebsite.com/v1/ 得到重定向到http://www.mywebsite.com

<rules>
          <rule name="Remove WWW" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^(api\.)(.*)$" />
          </conditions>
          <action type="Redirect" url="http://dev.blahblah.org.au{PATH_INFO}" redirectType="Permanent" />
          </rule>
    </rules>

【问题讨论】:

  • (.*) 不会捕获 /v1/ 吗?还是我错过了什么?
  • 你的设置应该已经是这样了。你有 url="^(.*)$" 匹配你的 v1/input="{HTTP_HOST}" pattern="^(api\.)(.*)$" 匹配 api.mywebsite.com

标签: asp.net regex api iis web-config


【解决方案1】:
<rule name="Remove WWW" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(api\.)(.*)$" />
        <add input="{PATH_INFO}" pattern="^/v(\d+)/(.*)$" />
    </conditions>
    <action type="Redirect" url="http://localhost{PATH_INFO}?v={C:1}" redirectType="Permanent" />
</rule>

【讨论】:

  • 这非常符合我们的需求。但在我们的例子中,唯一的区别是使用 action type="Rewrite" 进行传输而不是重定向。
猜你喜欢
  • 2018-08-25
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
  • 2012-05-27
相关资源
最近更新 更多